Python is a popular, easy-to-learn programming language created by Guido van Rossum in 1991. It is widely used in schools, industry and research because its syntax is simple and close to everyday English. Python is a high-level language, which means we do not have to worry about the internal details of the computer; we can focus on solving the problem. It is used to build websites, games, apps, and even systems that use artificial intelligence.
Python is an interpreted language, so the code is executed line by line by an interpreter instead of being compiled all at once. It is also a general-purpose language, meaning it can be used for many different kinds of tasks. Python's name comes from the comedy show "Monty Python's Flying Circus", not from the snake. In this chapter, we will learn how to install and use Python, write our first programs, use comments, and understand variables, operators and input and output.
To start programming in Python, we first need to install Python on the computer from its official website, python.org. Once installed, we can use two ways to write and run Python code. The first is the interactive shell, also called IDLE, where we type one command at a time and see the result immediately, shown by the prompt >>>. The second is the script mode, where we write a complete program in a file with the .py extension and run it all at once.
The interactive shell is very useful for trying out small pieces of code and testing ideas, because the result appears instantly after each line. For example, typing print("Hello") and pressing Enter immediately shows Hello. For a full program, we save the code in a file such as first.py and run it using the Run command or by typing the file name in the terminal. Understanding both modes helps us move from testing small ideas to writing complete programs.
The very first program that everyone writes in Python is the "Hello World" program. It is simply one line: print("Hello World"). The print function displays whatever is written inside the brackets on the screen. Text inside quotes is called a string. We can print numbers, text and even the results of calculations. For example, print(5 + 3) displays the number 8 on the screen.
Comments are lines in a program that are not executed by Python. They are used to explain what the code does, which makes the program easier to read and understand. In Python, a single-line comment begins with the hash symbol #. For example, # This program prints a greeting is a comment. Multi-line comments can be written using triple quotes """ """. Comments are very important in programming because they help us and other people understand the logic of the code.
A variable is a name given to a location in memory that stores a value. In Python, we create a variable simply by giving it a name and assigning a value with the equals sign. For example, age = 13 stores the value 13 in the variable age. The name of a variable should be meaningful, such as name, marks or price, so that the program is easy to read. Python variable names cannot contain spaces and cannot start with a number.
Variables can store different types of data. Text is stored as a string, such as "Anita". Whole numbers are stored as integers, such as 12. Numbers with a decimal point are floats, such as 12.5. A value can be only true or false, and it is called a boolean. Python is a dynamically typed language, which means we do not have to declare the type of a variable; Python decides it from the value we assign. A variable can even change its type if we assign a new value of a different type.
Programs are more useful when they can accept data from the user. The input function in Python reads a value typed by the user. For example, name = input("Enter your name: ") shows the message and stores what the user types in the variable name. Whatever the user types is read as a string, so if we need a number, we must convert it using int() or float(). For example, age = int(input("Enter age: ")).
Operators are symbols that perform operations on values. The arithmetic operators are + for addition, - for subtraction, * for multiplication, / for division, and % for modulus, which gives the remainder. Python also has ** for exponent, so 2 ** 3 means 2 raised to the power 3, which is 8. Output is given with the print function, and we can print several values in one statement by separating them with commas. Combining input, variables, operators and output lets us write complete, interactive programs.
| Data Type | Meaning | Example |
|---|---|---|
| String | Text | "Hello", 'Anita' |
| Integer | Whole number | 12, -5 |
| Float | Decimal number | 12.5, 3.14 |
| Boolean | True or False | True, False |
| Operator | Meaning | Example |
|---|---|---|
| + | Addition | 5 + 3 = 8 |
| - | Subtraction | 5 - 3 = 2 |
| * | Multiplication | 5 * 3 = 15 |
| / | Division | 6 / 3 = 2 |
| % | Modulus (remainder) | 7 % 3 = 1 |
| ** | Exponent (power) | 2 ** 3 = 8 |
Python is a simple, powerful and interpreted programming language that is perfect for beginners. We can run code in the interactive shell for quick tests or in script mode for complete programs. With the print and input functions, variables, data types and operators, we can already write programs that accept data, process it and display results. These building blocks prepare us for the next step, where we will study Python data types in more detail and then learn how to make decisions and repeat actions in our programs.