Szkoła Podstawowa nr 1 w Jarosławiu

🐍Introduction to Programming in Python
🇵🇱

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.


🧠1. What is a program and how does Python work?

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:

Running Python

You can use:

Python files are saved with the extension .py.


📦2. Variables in Python

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.

Important: the type of a variable depends on the value you assign to it. Python detects the type automatically.

Examples of variables

💻Example code:
x = 10
text = "Ala has a cat"
pi = 3.14

Reading values from the keyboard

⌨️Input from the user:
surname = input("Enter your surname: ")
number = int(input("Enter an integer number: "))
average = float(input("Enter your grade average: "))

Basic data types


3. Arithmetic operators

OperatorOperationExampleResult
+addition23 + 5679
-subtraction987 - 233754
*multiplication432 * 62592
/division55 / 318.33…
//integer division55 // 318
%remainder37 % 41

⌨️4. The input() function – reading data

The input() function is used to read data from the user.

👤Reading the age:
age = int(input("Enter your age: "))
print("Next year you will be:", age + 1)

🔀5. Conditional instructions – if / else

They are used to make decisions in a program.

Syntax

if condition:
    instruction
else:
    instruction

Comparison and logical operators

OperatorMeaning
==equal to
!=not equal to
<less than
>greater than
<=less than or equal to
>=greater than or equal to
andlogical AND
orlogical OR
notlogical NOT

Example

⚖️Comparing two numbers:
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.")

🔁6. Loops – for and while

Loops allow us to repeat the same instruction many times.

for loop

for i in range(5):
    print(i)

while loop

x = 0
while x < 3:
    print(x)
    x += 1

The range() function


📐7. Iterative algorithms

Iteration means repeating the same operation many times. The program performs it using a loop.

Steps of an algorithm – example with plot areas

  1. Set i = 1.
  2. Enter x.
  3. Enter y.
  4. Compute area = x * y.
  5. Display area.
  6. Increase i = i + 1.
  7. If i ≤ 5, go back to step 2.

Program

📏Calculating the area of 5 rectangles:
for i in range(5):
    x = int(input("Enter x: "))
    y = int(input("Enter y: "))
    area = x * y
    print("Area of the plot:", area)

🧩8. Functions in Python

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.

Function returning a value

def square(a):
    return a * a
result = square(5)
print(result)

Function without a return value

def greet():
    print("Hello!")

greet()

Function with a parameter – cube

def cube(a):
    return a ** 3

value = cube(3)
print(value) 

📚9. Lists in Python

A list is a collection of many values stored in one variable. Each element has an index (number) starting from 0.

Defining a list

days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
empty = [0] * 10

print(days)
print(empty)

Accessing elements

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])

Adding an element

numbers = []

numbers.append(10)
numbers.append(20)
numbers.append(30)

print("Current list:", numbers)

Looping through a list

animals = ["dog", "cat", "rabbit"]

for x in animals:
    print("Animals:", x)

📥10. Reading data into a list

1. Function for reading numbers into a list

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)

2. Function for printing the list

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)

🧪11. Extra tasks for students

1️⃣Task 1 — the greatest of three numbers

Read three numbers from the user and print the greatest one.

2️⃣Task 2 — number of vowels in a text

Read a text and count how many vowels it contains: a, e, i, o, u, y.

3️⃣Task 3 — function square_area(a)

Write a function that calculates the area of a square.

4️⃣Task 4 — shopping list

Create a list with 5 products and print them all using a loop.

5️⃣Task 5 — “Guess the number” game

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”.