Python Basics
Python Basics
Python is a powerful and easy-to-learn language. Let's get started with some
fundamental concepts.
1. Indentation
Indentation refers to the spaces at the beginning of a code line. In many programming
languages, indentation is used for readability, but in Python, it's very important.
Python uses indentation to indicate a block of code.
● You should use 4 spaces for indentation.
● You'll see this when we use if statements and for loops.
Example:
if 5 > 2:
print("Five is greater than two!") # This line is indented
print()
The print() function is used to display output to the screen. You can print text
(strings), numbers, or the values of variables.
● Text (strings) needs to be enclosed in single quotes (') or double quotes (").
Examples:
print("Hello, World!")
print('Python is fun!')
print(123)
print(4 + 5)
input()
The input() function allows you to get input from the user. It pauses your program and
waits for the user to type something and press Enter.
● The input() function always returns the user's input as a string. If you need it to be
a number, you'll have to convert it.
Example:
3. Variables
A variable is like a container that stores a value. You can give a variable a name and
then use that name to refer to the value it holds.
● Naming Rules:
○ Variable names can contain letters (a-z, A-Z), numbers (0-9), and the
underscore character (_).
○ They cannot start with a number.
○ Variable names are case-sensitive (myVariable is different from myvariable).
○ Choose descriptive names for your variables (e.g., user_age instead of x).
Assigning Values:
You use the equals sign (=) to assign a value to a variable.
Examples:
num1 = 15
num2 = 4
sum_result = num1 + num2
difference = num1 - num2
product = num1 * num2
quotient = num1 / num2
floor_quotient = num1 // num2
remainder = num1 % num2
power = num2 ** 3 # 4 to the power of 3
print("Sum:", sum_result)
print("Difference:", difference)
print("Product:", product)
print("Quotient:", quotient)
print("Floor Quotient:", floor_quotient)
print("Remainder:", remainder)
print("Power (num2^3):", power)
You can also use parentheses () to control the order of operations, just like in math.
The i in for i in range(...) is a variable that takes on the value of each number in the
sequence, one by one. You can name this variable anything you like (e.g., number,
count), but i is a common convention.
Examples:
You can also add an else block, which will run if the if condition is false.
Remember the indentation for the code inside if and else blocks!
Comparison Symbols
These are used to create conditions:
● == : Equal to (e.g., a == b)
● != : Not equal to (e.g., a != b)
● > : Greater than (e.g., a > b)
● < : Less than (e.g., a < b)
● >= : Greater than or equal to (e.g., a >= b)
● <= : Less than or equal to (e.g., a <= b)
Examples:
age = 15
if age >= 18:
print("You are an adult.")
else:
print("You are a minor.")
temperature = 25
if temperature > 30:
print("It's hot outside!")
elif temperature > 20: # 'elif' means 'else if' - checks another condition
print("It's a pleasant day.")
else:
print("It might be cold.")
# Example with numbers
num_a = 10
num_b = 20
if num_a == num_b:
print(f"{num_a} is equal to {num_b}")
elif num_a < num_b:
print(f"{num_a} is less than {num_b}")
else:
print(f"{num_a} is greater than {num_b}")
password_attempt = "12345"
correct_password = "password123"
if password_attempt == correct_password:
print("Access granted!")
else:
print("Access denied!")