ЁЯТ╗
тМия╕П
ЁЯЦ▒я╕П
ЁЯЦея╕П
ЁЯТ╛
тЖР Back to Dashboard
Font Size:

1. Introduction

Python is a simple, powerful and popular programming language. A programming language is a set of rules and symbols used to write instructions that a computer can follow. Python was created by Guido van Rossum in 1991, and its name comes from the British comedy show "Monty Python". Python is known for being easy to read because its code looks like simple English, which makes it an excellent first language for beginners. It is used for creating software, websites, games and for data analysis and artificial intelligence.

When we write instructions in a programming language, the text is called source code or a program. Python instructions are written in a file with the extension .py and are executed by the Python interpreter, which reads the code line by line and performs the actions. The interpreter needs to be installed on the computer. We can also write small Python commands directly in the Python Shell (also called IDLE), where each command is executed as soon as we press Enter. This chapter explains Python basics: the shell, print statements, variables, data types, operators, input, conditional statements and loops.

2. The Python Shell and IDLE

When Python is installed, it comes with an interactive program called IDLE (Integrated Development and Learning Environment). In the Python Shell we see the prompt >>>, where we can type a command and press Enter. For example, typing print("Hello") and pressing Enter immediately shows Hello. For a longer program with many lines, we write the code in the Script Editor window of IDLE, save the file with the .py extension, and then run it with the Run menu or by pressing F5. The output appears in the shell window below.

3. The print Statement

The print() function is the most basic Python instruction; it displays text or values on the screen. Text to be printed is written inside quotes, either single quotes like 'Hello' or double quotes like "Hello". For example, print("My name is Rahul") displays My name is Rahul. We can print more than one item by separating them with commas: print("Sum is", 5 + 3). We can also print a blank line by using print() without any content. The text inside quotes is called a string.

4. Variables

A variable is a named container in which we store a value in the computer's memory. We create a variable by writing its name, an equals sign and the value, for example age = 12 or name = "Anita". The equals sign here is called the assignment operator because it assigns a value to the variable. A variable name should be meaningful, and it should not start with a number or contain spaces. For example, marks = 85 stores the value 85 in the variable named marks. We can use the variable later in print statements, for example print(age). The value of a variable can be changed at any time by assigning a new value to it.

5. Data Types

The type of data stored in a variable is called its data type. Python has several basic data types. An integer is a whole number such as 12 or -5. A float is a number with a decimal point, such as 12.5. A string is a sequence of characters in quotes, such as "Hello" or 'Anita'. A boolean value is either True or False, used for true or false conditions. For example, x = 10 makes x an integer, y = 10.5 makes y a float, and z = "Hello" makes z a string. Python decides the type automatically from the value we assign, so we do not need to declare the type.

6. Operators in Python

Operators are symbols that perform calculations on values, which are called operands. The arithmetic operators are + (addition), - (subtraction), * (multiplication), / (division), // (integer division that gives a whole-number quotient), % (modulus that gives the remainder) and ** (power). For example, 5 + 3 gives 8, 7 // 2 gives 3, 7 % 2 gives 1 and 2 ** 3 gives 8. The relational or comparison operators are == (equal to), != (not equal to), > (greater than), < (less than), >= (greater than or equal) and <= (less than or equal); they give True or False. The logical operators are and, or and not.

7. Taking Input

The input() function lets the user type a value while the program is running. For example, name = input("Enter your name: ") displays the message and stores whatever the user types into the variable name. The value returned by input() is always a string, even if the user types a number. So to use the entered value in a calculation, we must convert it with the int() function (for a whole number) or the float() function (for a decimal). For example, age = int(input("Enter your age: ")) converts the input to an integer.

8. Conditional Statements

A conditional statement lets the program make decisions. The if statement checks a condition, and if it is True, it runs the indented block below it. For example:

if marks >= 40:
    print("You have passed")

The if...else statement runs one block when the condition is True and a different block when it is False. The if...elif...else statement checks several conditions one by one. For example, if x > 0 prints "Positive", elif x < 0 prints "Negative", else prints "Zero". The block of statements under a condition must be indented (pressed Tab or spaces), because Python uses indentation to know which statements belong to which condition.

9. Loops

A loop repeats a block of statements several times. The for loop runs the block a fixed number of times or over a list of items. For example, for i in range(5): repeats the indented block 5 times with i having values 0, 1, 2, 3 and 4. The range() function generates the sequence of numbers. The while loop repeats the block as long as a condition is True, for example while count < 3:. Loops save us from writing the same instruction many times. We must be careful with while loops to make the condition become False, otherwise the loop runs forever.

Quick Revision Tables

Table 1: Arithmetic Operators

Operator Meaning Example and result
+ Addition 5 + 3 = 8
- Subtraction 5 - 3 = 2
* Multiplication 5 * 3 = 15
/ Division 7 / 2 = 3.5
// Integer division 7 // 2 = 3
% Modulus (remainder) 7 % 2 = 1
** Power 2 ** 3 = 8

Table 2: Basic Data Types

Data type Meaning Example
int Whole number 12, -5
float Decimal number 12.5
str String in quotes "Hello"
bool True or False True

Mind Map

flowchart TD A["Python Basics"] --> B["Python Shell and IDLE"] A --> C["print statement"] A --> D["Variables"] A --> E["Data Types"] A --> F["Operators"] A --> G["input function"] A --> H["if conditions"] A --> I["Loops"] D --> D1["name = value"] E --> E1["int, float, str, bool"] F --> F1["Arithmetic + - * /"] F --> F2["Relational < > =="] H --> H1["if, elif, else"] I --> I1["for loop"] I --> I2["while loop"]

Important Diagrams (SVG)

Diagram 1: Flow of a Python Program

Flow of a Python Program START INPUT - age = int(input()) Get the user's age DECISION - if age >= 18 Is the condition True? True False print("Adult") Block 1 runs print("Minor") Else block runs Golden Rule: if checks a condition, runs the True block or the else block, using indentation. Indented lines belong to the condition; Python reads programs line by line.

Diagram 2: Python Components

PYTHON VARIABLES age = 12 Store values in memory DATA TYPES int, float, str, bool Types of stored data OPERATORS + - * / // % ** Do calculations FUNCTIONS print() display input() gets values CONDITIONS if, elif, else Make decisions LOOPS for loop, while loop Repeat statements Example: name = input("Name: ") ; print("Hello", name) ; if age > 12 : print("Teen") Golden Rule: Python code is read line by line; variables store values and print() shows them. Save programs with the .py extension and run them in IDLE with F5.

Common Mistakes

  1. Forgetting quotes in a print string: print(Hello) gives an error; it must be print("Hello").
  2. Writing == as = in a condition. = assigns a value; == compares two values.
  3. Forgetting indentation under if and loops, so Python reports an indentation error.
  4. Using 7 / 2 and expecting 3. In Python / gives 3.5; // gives the integer quotient 3.
  5. Forgetting to convert input with int() and then adding strings, causing an error.
  6. Using spaces or numbers at the start of a variable name, which is not allowed.
  7. Writing the modulo operator as % but explaining it wrongly; % gives the remainder.
  8. Confusing string values with numbers, so "5" + 3 fails because one is a string.
  9. Forgetting to make a while condition become False, causing an infinite loop.

Exam Tips

  1. Learn the output of simple programs, for example print("Hello") gives Hello.
  2. Memorise all the arithmetic operators and their results, especially // and %.
  3. Write the difference between = (assignment) and == (comparison) clearly.
  4. Know the four basic data types: int, float, str and bool, with one example each.
  5. Practise writing a small if...else program and describe what it does.
  6. Remember that Python was created by Guido van Rossum and files end with .py.
  7. Explain that indentation is essential in Python to group statements under conditions and loops.

Conclusion

Python is a simple and powerful programming language that is perfect for beginners. We write programs in the IDLE shell or editor, use print() to show output, store values in variables, and work with data types like integers, floats, strings and booleans. Arithmetic, relational and logical operators perform the calculations, the input() function gets values from the user, and if conditions and loops let the program make decisions and repeat actions. With these basics, a student can already write small useful programs. The next chapter develops logical thinking, which is the real skill behind all programming.