Python is one of the easiest programming languages — clear, intuitive and very popular. It is used for building websites, data analysis, artificial intelligence, automation and many other things. The material below is a complete introduction to the topic and an extension with practical examples.
The source code of a program is a set of instructions that the computer should execute. Python is an interpreted language — that means the interpreter executes the program line by line.
Basic stages of creating a program:
You can use:
Python files are saved with the extension .py.
A variable is a part of the computer’s memory with a unique address, where a value is stored. This value can change while the program is running.
x = 10 text = "Ala has a cat" pi = 3.14
surname = input("Enter your surname: ")
number = int(input("Enter an integer number: "))
average = float(input("Enter your grade average: "))
| Operator | Operation | Example | Result |
|---|---|---|---|
| + | addition | 23 + 56 | 79 |
| - | subtraction | 987 - 233 | 754 |
| * | multiplication | 432 * 6 | 2592 |
| / | division | 55 / 3 | 18.33… |
| // | integer division | 55 // 3 | 18 |
| % | remainder | 37 % 4 | 1 |
The input() function is used to read data from the user.
age = int(input("Enter your age: "))
print("Next year you will be:", age + 1)
They are used to make decisions in a program.
if condition:
instruction
else:
instruction
| Operator | Meaning |
|---|---|
| == | equal to |
| != | not equal to |
| < | less than |
| > | greater than |
| <= | less than or equal to |
| >= | greater than or equal to |
| and | logical AND |
| or | logical OR |
| not | logical NOT |
a = int(input("Enter the first number: "))
b = int(input("Enter the second number: "))
if a > b:
print("The first number is greater.")
else:
print("The second number is greater or equal.")
Loops allow us to repeat the same instruction many times.
for i in range(5):
print(i)
x = 0
while x < 3:
print(x)
x += 1
range(5) → 0…4range(10, 15) → 10…14range(1, 20, 2) → odd numbers from 1 to 19Iteration means repeating the same operation many times. The program performs it using a loop.
i = 1.x.y.area = x * y.area.i = i + 1.i ≤ 5, go back to step 2.for i in range(5):
x = int(input("Enter x: "))
y = int(input("Enter y: "))
area = x * y
print("Area of the plot:", area)
A function is a subprogram — a block of code that performs a specific task. Functions help to organise the code and make programs easier to read.
def square(a):
return a * a
result = square(5)
print(result)
def greet():
print("Hello!")
greet()
def cube(a):
return a ** 3
value = cube(3)
print(value)
A list is a collection of many values stored in one variable. Each element has an index (number) starting from 0.
days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] empty = [0] * 10 print(days) print(empty)
days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
print("Number of days in January:", days[0])
print("Number of days in December:", days[11])
numbers = []
numbers.append(10)
numbers.append(20)
numbers.append(30)
print("Current list:", numbers)
animals = ["dog", "cat", "rabbit"]
for x in animals:
print("Animals:", x)
def read_data(lst):
for i in range(len(lst)):
lst[i] = int(input("Enter a number: "))
numbers = [0, 0, 0]
read_data(numbers)
print("Entered numbers:", numbers)
def read_data(lst):
for i in range(len(lst)):
lst[i] = int(input("Enter a number: "))
size = int(input("How many numbers do you want to enter? "))
data = [0] * size
read_data(data)
print("Twoje dane:", data)
Read three numbers from the user and print the greatest one.
Read a text and count how many vowels it contains: a, e, i, o, u, y.
square_area(a)Write a function that calculates the area of a square.
Create a list with 5 products and print them all using a loop.
Choose a random number from 1 to 20. The user tries to guess it. After each attempt, print information: “too low”, “too high” or “correct”.