Summary
This summary covers the essential Python topics for beginners: variables, data types, input/output, operators, type conversion, and conditional statements.
Variables and Data Types
Variables
- Variables are containers for values stored in memory.
- Syntax:
variable_name = valueVariable names rules:
- Must start with a letter (
a-zA-Z) or underscore_. - Can contain letters, digits, and underscores.
- Cannot be a reserved keyword.
- Case-sensitive (
myVar≠myvar).
x = 3num_1 = 30_second = 13π = 3.1415علي = "Me"Note
Reserved keywords are words that have a special meaning in Python and cannot be used as variable names. You can find the list of reserved keywords here.
Primitive Data Types
| Type | Description |
|---|---|
int | Integer numbers (unlimited precision) |
float | Numbers with decimals |
str | Sequence of characters |
bool | Boolean values: True or False |
None | Represents no value |
complex | Complex numbers with real and imaginary parts |
Examples:
# Integerx = 10y = -200
# Floatpi = 3.14y = 0.0
# Stringname = "Alice"
# Booleanflag = True
# Nonevalue = None
# Complexz = 1.23 + 4.56jNumber Literals
- Underscores can be used to group digits:
w = 1_000_000_000y = 1_234.5_678z = 22_333 + 5_123.6_789jInput and Output
Print Function
- Syntax:
print(value, ..., sep=' ', end='\n') - Default behavior: separates values with a space and ends with a newline.
print("Hello", "World") # Hello Worldprint("Hello", "World", sep='') # HelloWorldprint("Hello", end='!') # Hello!Input Function
- Syntax:
input(prompt='') - Always returns a string. Use type conversion for numbers.
- Prompt is the string that is displayed to the user, which is optional.
name = input("What's your name? ")age = int(input("Enter your age: "))print("Hello", name, "you are", age, "years old")Operators
Arithmetic Operators
| Operator | Description | Example |
|---|---|---|
+ | Addition | 2 + 3 = 5 |
- | Subtraction | 5 - 2 = 3 |
* | Multiplication | 2 * 3 = 6 |
/ | Division (always float) | 5 / 2 = 2.5 |
// | Floor division | 5 // 2 = 2 |
% | Modulo (remainder) | 5 % 2 = 1 |
** | Power | 2 ** 3 = 8 |
- Floor division returns
floatif any operand is float. - Relation:
x % y = x - (x // y) * y
Assignment Operators
=assigns a value to a variable.- Augmented operators:
+=, -=, *=, /=, //=, %=, **=
x = 10x += 5 # x = 15x *= 2 # x = 30Comparison Operators
| Operator | Description |
|---|---|
== | Equal |
!= | Not equal |
> | Greater than |
< | Less than |
>= | Greater than or equal |
<= | Less than or equal |
- Comparison chaining:
x = 10y = 20z = 30x < y < z # Truex != y < z # TrueLogical Operators
| Operator | Description |
|---|---|
and | True if both operands are true |
or | True if at least one operand is true |
not | Negates the boolean value |
- Short-circuit behavior:
print(False and print("Hi")) # False, "Hi" not printedprint(True or print("Hi")) # True, "Hi" not printedType Conversion (Casting)
- Convert one type to another:
| Function | Description |
|---|---|
int(x) | Convert to integer |
float(x) | Convert to floating-point number |
str(x) | Convert to string |
bool(x) | Convert to boolean |
complex(x) | Convert to complex number |
Examples:
x = "123"y = int(x) # 123z = float(x) # 123.0b = bool("") # Falsec = complex(2, 3) # 2 + 3jConditional Statements
Basic If
age = 18if age >= 18: print("You are an adult")If-Else
age = 16if age >= 18: print("Adult")else: print("Minor")If-Elif-Else
age = 16if age >= 18: print("Adult")elif age >= 13: print("Teenager")else: print("Child")Nested Conditionals
a, b = 10, 5if a > b: if a > 0: print("a is positive and greater than b")- Alternative using
and:
if a > b and a > 0: print("a is positive and greater than b")Conditional Expression (Ternary Operator)
result = a if a > b else b- Single-line shorthand for
if-else. elseis mandatory.
