Python Basics

Variables and Data Types

In Python, you don’t need to declare a variable type explicitly. The interpreter assigns the type automatically.

Basic Data Types in Python:

Integers: x = 10

Floats: x = 3.14

Strings: x = “Hello”

Booleans: x = True

Example:

x = 10  # Integer
y = 3.14  # Float
name = "Alice"  # String
is_valid = True  # Boolean

Operators in Python Python supports various operators:

Arithmetic Operators:

+, -, *, /, //, %, **

Comparison Operators:

'' ==, !=, >, <, >=, <=

Logical Operators:

and, or, not

Example:

x = 10
y = 5
print(x + y)  # Output: 15
print(x > y)   # Output: True

Conditional Statements Conditional statements allow you to make decisions in your code.

if x > y:
    print("x is greater than y")
else:
    print("x is not greater than y")

Output

x is greater than y

Loops in Python Python has two main types of loops:

  1. For Loop: Used to iterate over a sequence.
  2. While Loop: Executes as long as a condition is true.

Example:

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

Output:

0
1
2
3
4