Python is a popular programming language known for its simplicity and readability. Even if you have no coding background, you can start learning Python easily.
This tutorial covers the basics:
- Assignment: Storing data in variables.
- Expressions: Performing calculations or evaluating logic.
- Print Statements: Displaying information on the screen.
1. Assignment in Python
An assignment means giving a value to a variable. A variable is like a container where you can store data, such as numbers or text.
Syntax:
variable_name = value
Examples:
# Assigning numbers to variables
x = 10
y = 20
# Assigning text (called a string) to a variable
name = "Alice"
# Assigning a decimal number
price = 99.99
# Assigning a calculation (expression)
total = x + y
Explanation:
x
now holds the value10
.name
holds the text"Alice"
.total
is calculated as10 + 20
, so its value is30
.
2. Expressions in Python
An expression is anything that evaluates to a value. It can involve numbers, text, or even logical conditions.
Mathematical Expressions:
a = 5
b = 3
# Addition
sum_result = a + b # 8
# Subtraction
difference = a - b # 2
# Multiplication
product = a * b # 15
# Division
quotient = a / b # 1.6667
Logical Expressions:
Logical expressions compare values and return True
or False
.
x = 10
y = 20
is_equal = (x == y) # False
is_greater = (x > y) # False
is_smaller = (x < y) # True
Combining Expressions:
You can mix expressions together.
final_value = (x + y) * 2 - 5 # 55
3. Print Statements in Python
The print()
function is used to display information on the screen. This is how your program communicates with you!
Basic Printing:
print("Hello, World!") # Displays: Hello, World!
Printing Variables:
name = "Alice"
age = 25
print("Name:", name) # Displays: Name: Alice
print("Age:", age) # Displays: Age: 25
Combining Text and Variables:
price = 99.99
print("The price is:", price) # Displays: The price is: 99.99
Using f-strings for Better Formatting:
An f-string allows you to insert variables directly into text.
name = "Alice"
age = 25
print(f"My name is {name} and I am {age} years old.")
# Displays: My name is Alice and I am 25 years old.
Putting It All Together
Let’s combine assignment, expressions, and print statements into a full example.
# Assign values
x = 10
y = 5
# Perform calculations
sum_result = x + y
product_result = x * y
# Print results
print("Sum of x and y is:", sum_result) # Displays: Sum of x and y is: 15
print("Product of x and y is:", product_result) # Displays: Product of x and y is: 50
# Combine with f-strings
print(f"The sum of {x} and {y} is {sum_result}, and their product is {product_result}.")
# Displays: The sum of 10 and 5 is 15, and their product is 50.
Sample Outputs
Here’s what you’d see on your screen if you ran the above code:
Sum of x and y is: 15
Product of x and y is: 50
The sum of 10 and 5 is 15, and their product is 50.
Practice Problems
Try these on your own to solidify your understanding:
- Assign your favorite food to a variable and print it.
- Assign two numbers, calculate their difference, and print it.
- Use
f-strings
to create a message like: “I scored 90% in math.”