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.
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.
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.
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.
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.
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.
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.
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.
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.
| 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 |
| Data type | Meaning | Example |
|---|---|---|
| int | Whole number | 12, -5 |
| float | Decimal number | 12.5 |
| str | String in quotes | "Hello" |
| bool | True or False | True |
print(Hello) gives an error; it must be print("Hello").== as = in a condition. = assigns a value; == compares two values.7 / 2 and expecting 3. In Python / gives 3.5; // gives the integer quotient 3."5" + 3 fails because one is a string.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.