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

1. Introduction

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.

2. Setting Up Python and the Python Shell

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.

3. Your First Program and Comments

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.

4. Variables and Data

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.

5. Input, Output and Operators

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.

Quick Revision Tables

Table 1: Python Data Types

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

Table 2: Python Operators

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

Mind Map

flowchart TD A["Introduction to Python"] --> B["About Python"] A --> C["Setting Up"] A --> D["First Program"] A --> E["Variables"] A --> F["Input and Output"] A --> G["Operators"] B --> B1["Created by Guido van Rossum"] B --> B2["Interpreted language"] C --> C1["Interactive shell"] C --> C2["Script mode with .py"] E --> E1["name = value"] E --> E2["String, Integer, Float"] F --> F1["print() output"] F --> F2["input() input"] G --> G1["+ - * / % **"]

Important Diagrams (SVG)

Diagram 1: Python Program Flow

Flow of a Python Program START Program begins INPUT name = input('Enter name: ') PROCESS age = age + 1 OUTPUT print('Hello', name) END Golden Rule: Every program takes input, processes it and produces output, in that order. input() reads data, variables and operators process it, and print() shows the result.

Diagram 2: How Python Runs Code

How Python Runs Your Code SOURCE CODE first.py Written by you INTERPRETER Reads line by line Executes each line OUTPUT Shown on screen print() results Golden Rule: Python is interpreted - it reads and executes your code line by line. Use the interactive shell for quick tests and script mode for complete programs.

Common Mistakes

  1. Forgetting the brackets in print. The print function must be written as print("Hello") with brackets.
  2. Using double quotes instead of single quotes inconsistently. Python strings can use either, but they must match.
  3. Writing comments without the # symbol. A single-line comment must begin with the hash symbol.
  4. Giving a variable a name that starts with a number, like 1name. Variable names cannot start with a number.
  5. Forgetting that input() returns a string. A number must be converted with int() or float() before arithmetic.
  6. Confusing the division operator. Use a single forward slash / for division, not a backslash.
  7. Believing that Python is a compiled language. Python is an interpreted language executed line by line.
  8. Using a space in a variable name. Variable names cannot contain spaces; use an underscore like first_name.
  9. Thinking that the interactive shell is for full programs. It is for quick tests, while script mode runs whole programs.
  10. Forgetting to save the file with the .py extension in script mode.

Exam Tips

  1. Remember that Python was created by Guido van Rossum and is an interpreted, high-level language.
  2. Write the first program as print("Hello World").
  3. Know the data types: string, integer, float and boolean, with one example of each.
  4. Learn the arithmetic operators +, -, *, /, % and ** and what each does.
  5. Remember that input() returns a string and needs int() or float() to convert it to a number.
  6. Be able to write a program that takes two numbers as input and prints their sum.
  7. Remember that comments begin with # and are not executed by Python.

Conclusion

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.