Chapter 1 Summary

A summary of the essential Python topics for beginners: variables, data types, input/output, operators, type conversion, and conditional statements.

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 = value

Variable 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 (myVarmyvar).
x = 3
num_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

TypeDescription
intInteger numbers (unlimited precision)
floatNumbers with decimals
strSequence of characters
boolBoolean values: True or False
NoneRepresents no value
complexComplex numbers with real and imaginary parts

Examples:

# Integer
x = 10
y = -200
# Float
pi = 3.14
y = 0.0
# String
name = "Alice"
# Boolean
flag = True
# None
value = None
# Complex
z = 1.23 + 4.56j

Number Literals

  • Underscores can be used to group digits:
w = 1_000_000_000
y = 1_234.5_678
z = 22_333 + 5_123.6_789j

Input and Output

  • Syntax: print(value, ..., sep=' ', end='\n')
  • Default behavior: separates values with a space and ends with a newline.
print("Hello", "World") # Hello World
print("Hello", "World", sep='') # HelloWorld
print("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

OperatorDescriptionExample
+Addition2 + 3 = 5
-Subtraction5 - 2 = 3
*Multiplication2 * 3 = 6
/Division (always float)5 / 2 = 2.5
//Floor division5 // 2 = 2
%Modulo (remainder)5 % 2 = 1
**Power2 ** 3 = 8
  • Floor division returns float if any operand is float.
  • Relation: x % y = x - (x // y) * y

Assignment Operators

  • = assigns a value to a variable.
  • Augmented operators: +=, -=, *=, /=, //=, %=, **=
x = 10
x += 5 # x = 15
x *= 2 # x = 30

Comparison Operators

OperatorDescription
==Equal
!=Not equal
>Greater than
<Less than
>=Greater than or equal
<=Less than or equal
  • Comparison chaining:
x = 10
y = 20
z = 30
x < y < z # True
x != y < z # True

Logical Operators

OperatorDescription
andTrue if both operands are true
orTrue if at least one operand is true
notNegates the boolean value
  • Short-circuit behavior:
print(False and print("Hi")) # False, "Hi" not printed
print(True or print("Hi")) # True, "Hi" not printed

Type Conversion (Casting)

  • Convert one type to another:
FunctionDescription
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) # 123
z = float(x) # 123.0
b = bool("") # False
c = complex(2, 3) # 2 + 3j

Conditional Statements

Basic If

age = 18
if age >= 18:
print("You are an adult")

If-Else

age = 16
if age >= 18:
print("Adult")
else:
print("Minor")

If-Elif-Else

age = 16
if age >= 18:
print("Adult")
elif age >= 13:
print("Teenager")
else:
print("Child")

Nested Conditionals

a, b = 10, 5
if 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.
  • else is mandatory.

Course Progress

Section 7 of 17

Back to Course