Содержание:
Python Programming: Module 1 – Theory
Introduction to Python, Syntax, and Basic Operations | For English‑speaking students
📖 What is a Program?
A computer program is a list of instructions for the computer. Instructions can be:
- Read data from the keyboard.
- Perform arithmetic calculations (
+,-,*,/). - Display information on the screen.
Programs are written in programming languages — a set of rules that allows humans to write instructions a computer can understand and execute. The text of a program is called source code.
Examples of programming languages: Python, C++, Java, C#, Ruby, PHP.
Compiled languages (C, C++, Java): The entire source code is translated into machine code before execution. Requires a compilation step — errors are caught early.
Source → Compiler → Machine Code → Execute
Interpreted languages (Python, Ruby, JavaScript): Code is executed line by line by an interpreter. No separate compilation — easier to test and debug.
Source → Interpreter → Execute line by line
💡 Подробнее о Python
Python was created by Guido van Rossum and is widely used in science and IT. It is named after the show «Monty Python’s Flying Circus», not the snake.
Key characteristics: High-level, Interpreted, Object-oriented, General-purpose, Open-source.
Advantages: Platform-independent, simple syntax, huge library of classes, dynamic typing.
Disadvantages: Relatively slow execution speed.
Interesting fact: Python consistently ranks among the top 3 programming languages in popularity (TIOBE index).
📝 Python Syntax Basics
Python has a clean and readable syntax. Key rules:
- Indentation defines code blocks (no curly braces
{ }). - Case-sensitive —
myVar≠myvar. - Comments start with
#. - Statements are usually on separate lines.
- Colon (
:) indicates the start of an indented block (e.g., afterif,for). - No semicolons required at line ends.
💡 Open an example
# This is a comment name = "Alice" if name == "Alice": print("Hello, Alice!") # indented block |
🖨️ Output: print()
The print() function displays output to the console. It can take any number of arguments.
💡 Open an example
print("Hello, World!") print(42) print("The answer is", 42) # Output: The answer is 42 |
Quotes can be either single or double: print('Python') and print("Python") work the same.
Calling print() with no arguments creates an empty line.
The optional sep parameter allows you to specify a set of characters that will separate arguments when outputting data.
💡 Open an example
print('a', 'b', 'c') # a b c print('d', 'e', 'f') # d e f print('a', 'b', sep='*') # a*b print('d', 'e', 'f', sep='**') # d**e**f |
By default, print() adds a newline after output. The optional end parameter allows you to set a special ending character.
💡 Open an example
print('a', 'b', 'c') # a b c print('d', 'e', 'f') # d e f print('a', 'b', 'c', end='@') # a b c@ print('d', 'e', 'f', end='@@') # d e f@@ |
💡 Open an example
print("a", "b", "c", sep="*") # a*b*c print("Hello", end=" ") # Hello World print("World") print("One", "Two", "Three", sep="-", end="\n") # One-Two-Three |
Notes:
- Default values:
sep=' '(space),end='\n'(newline). \nis the newline character.- To remove all additional output characters:
print('a', 'b', 'c', sep='', end='')
⌨️ Input: input()
The input() function reads a line of text from the user. Its syntax is input([prompt]). It always returns a string — even if the user types digits.
💡 Open an example
name = input("Enter your name: ") print("Hello,", name) # Example run: # Enter your name: Alice # Hello, Alice |
Important: input() always returns a string!
age = input("Enter age: ") # "20" (string) # age + 1 # ERROR! Can't add string and int |
📦 Variables and Data Types
A variable is a named reference to a value. Assignment uses the = operator: variable_name = value.
Python is dynamically typed — the type is determined by the assigned value.
💡 Open an example
x = 5 # integer x = "hello" # now a string print(x) # hello |
📌 Variable Naming Rules
- Can contain only Latin letters
a-z,A-Z, digits, and underscores (_). - Cannot start with a digit.
- Should reflect its purpose whenever possible.
Correct names: name, my_variable, sum, TotalAmount, qwerty12my.
Incorrect names: 5name (starts with digit), nepeMeHHaa (non‑Latin), surname$% (invalid characters).
📌 Style (PEP 8)
Use lower_case_with_underscores for variables and functions.
💡 Open an example
name = "Alice" # string <class 'str'> age = 20 # integer <class 'int'> pi = 3.14159 # float <class 'float'> is_student = True # boolean <class 'bool'> |
Built‑in types include:
int— integer numbers (e.g.,42,-7)float— floating‑point numbers (e.g.,3.14,-0.5)str— strings (e.g.,"hello")bool— boolean (True/False)list— ordered, mutable collection (e.g.,[1, 2, 3])tuple— ordered, immutable collection (e.g.,(1, 2, 3))dict— key‑value pairs (e.g.,{"name": "Alice"})set— unordered collection of unique items (e.g.,{1, 2, 3})
Interesting fact: In Python, variables are references to objects, not memory containers. Even int and float are objects — everything is an object.
💡 Open an example
num1 = 7 num2 = 10 num3 = num1 + num2 print(num3) # 17 # Underscore as separator for readability: big_num = 25_000_000 print(big_num) # 25000000 |
Note: int is short for integer. Python supports long arithmetic — integer variables have no limits.
🧮 Arithmetic Operations
Python supports the following arithmetic operators:
| Operator | Description | Example | Result |
|---|---|---|---|
+ |
Addition | 3 + 2 |
5 |
- |
Subtraction | 3 - 2 |
1 |
* |
Multiplication | 3 * 2 |
6 |
/ |
True division | 3 / 2 |
1.5 |
** |
Exponentiation | 3 ** 2 |
9 |
// |
Floor division | 3 // 2 |
1 |
% |
Modulo (remainder) | 3 % 2 |
1 |
💡 Open an example
a = 3 b = 2 print(a + b) # 5 print(a - b) # 1 print(a * b) # 6 print(a / b) # 1.5 |
Operator precedence (from highest to lowest):
***,//,%+,-
Use parentheses () to override precedence.
💡 Open an example
print(2 + 3 * 4) # 14 print((2 + 3) * 4) # 20 print(10 // 3) # 3 print(10 % 3) # 1 |
💡 Open an example
a = 13.5 b = 2.0 print(a + b) # 15.5 print(a - b) # 11.5 print(a * b) # 27.0 print(a / b) # 6.75 print(a ** b) # 182.25 |
You can also use // and % with floats:
print(13.5 // 2.0) # 6.0 print(13.5 % 2.0) # 1.5 |
🔄 Type Conversion
You can convert between types using built‑in functions:
int(x)— convert to integerfloat(x)— convert to floatstr(x)— convert to string
This is necessary when reading numbers with input() (which returns a string).
💡 Open an example
age = '1992' year = int(age) # year is an integer type grade = int(input()) # grade is an integer type |
int() converts the specified value into an integer.
💡 Open an example
num1 = int(input()) num2 = int(input()) print(num1 + num2) |
💡 Open an example
num = float(input()) # conversion of the read string n = float('1.2345') |
Implicit conversion: any integer can be used where a float is expected.
Explicit conversion: a float cannot be used where an integer is expected — use int().
💡 Open an example
num1 = 17.89 num2 = -13.56 num3 = int(num1) # 17 num4 = int(num2) # -13 print(num3) print(num4) |
The int() function simply discards the fractional part.
💡 Open an example
# String to int num = int("123") # 123 # String to float num = float("3.14") # 3.14 # Int to string text = str(123) # "123" # Float to int (truncates) num = int(3.99) # 3 |
🔢 Working with Digits
Using the // (floor division) and % (modulo) operations, you can extract individual digits from a number. This technique is used in digital clocks, check digit algorithms, and more.
📌 Integer Division and Remainder
//— calculates the integer part of division (how many times the divisor «fits» into the number).%— calculates the remainder after division.
Examples:
10 // 2 = 5;22 // 7 = 3;65 // 10 = 610 % 2 = 0;22 % 7 = 1;65 % 10 = 5
💡 Open an example
num = 17 a = num % 10 # 7 (last digit) b = num // 10 # 1 (first digit) print(a) print(b) |
💡 Open an example
num = 754 a = num % 10 # 4 (last digit) b = (num % 100) // 10 # 5 (middle digit) c = num // 100 # 7 (first digit) print(a) print(b) print(c) |
General formula for the k‑th digit (from right, 0‑based): (n // 10**k) % 10
Any number can be disassembled into digits using powers of 10. This rule is called the standard form of a number:
123 = 1*100 + 2*10 + 3
Using the same divisor K and operations // and %, you can restore the number by the formula: quotient * K + remainder.
💡 Open an example
# 10 // 2 = 5; 10 % 2 = 0 => 5 * 2 + 0 = 10 # 22 // 7 = 3; 22 % 7 = 1 => 3 * 7 + 1 = 22 # 65 // 10 = 6; 65 % 10 = 5 => 6 * 10 + 5 = 65 |
- Hundreds (first digit):
number // 100(e.g.,123 // 100 = 1) - Tens (second digit):
(number % 100) // 10(e.g.,123 % 100 = 23, then23 // 10 = 2) - Units (third digit):
number % 10(e.g.,123 % 10 = 3)
💡 Open an example
num = 754 last = num % 10 # 4 middle = (num // 10) % 10 # 5 first = num // 100 # 7 print(first, middle, last) # 7 5 4 |