Variables and Data Types
Understanding Variables and Their Purpose
Variables are fundamental components of programming languages, including Python. They are used to store and manipulate data during program execution. This topic will introduce you to variables, their purpose, and how to work with them in Python.
Examples of Coding
Example 1:
Assigning and Printing Variables
name = "John"
age = 25
height = 1.75
print("Name:", name)
print("Age:", age)
print("Height:", height)
Output:
Name: John
Age: 25
Height: 1.75
Example 2:
Updating Variable Values
count = 5
count = count + 1
print("Count:", count)
Output:
Count: 6
Example 3:
Variable Types and Casting
x = 5
y = 3.14
z = "Hello"
print(type(x))
print(type(y))
print(type(z))
x = str(x)
y = int(y)
z = float(z)
print(type(x))
print(type(y))
print(type(z))
Output:
<class 'int'>
<class 'float'>
<class 'str'>
<class 'str'>
<class 'int'>
<class 'float'>
Working with different data types (strings, numbers, booleans)
Data types are an essential concept in programming, as they determine the type of data that can be stored and manipulated. Python supports various data types, including strings, numbers (integers and floats), and booleans. This topic will cover the basics of working with these data types in Python.
Examples
Example 1:
Working with Strings
name = "John"
age = 25
# Concatenation
message = "My name is " + name + " and I am " + str(age) + " years old."
print(message)
# String methods
print(name.upper())
print(name.lower())
print(name.startswith("J"))
print(name.endswith("n"))
Output:
My name is John and I am 25 years old.
JOHN
john
True
True
Example 2:
Working with Numbers
x = 5
y = 3.14
# Arithmetic operations
sum = x + y
difference = x - y
product = x * y
division = x / y
remainder = x % y
print("Sum:", sum)
print("Difference:", difference)
print("Product:", product)
print("Division:", division)
print("Remainder:", remainder)
Output:
Sum: 8.14
Difference: 1.86
Product: 15.7
Division: 1.592356687898089
Remainder: 1.86
Example 3:
Working with Booleans
is_true = True
is_false = False
print(is_true)
print(is_false)
# Logical operations
print(is_true and is_false)
print(is_true or is_false)
print(not is_true)
Output:
True
False
False
True
False
Basic Operations and Expressions
Operations and expressions are fundamental components of programming. They allow you to perform calculations, manipulate data, and make decisions based on conditions. This topic will cover the basic operations and expressions in Python, including arithmetic operations, assignment operators, comparison operators, logical operators, and conditional expressions.
Resource : https://realpython.com/python-operators-expressions/
Example 1: Arithmetic Operations
x = 5
x += 3 # Equivalent to x = x + 3
x -= 2 # Equivalent to x = x - 2
x *= 4 # Equivalent to x = x * 4
x /= 2 # Equivalent to x = x / 2
x %= 3 # Equivalent to x = x % 3
print("x:", x)
Example 2: Assignment Operators
x = 5
x += 3 # Equivalent to x = x + 3
x -= 2 # Equivalent to x = x - 2
x *= 4 # Equivalent to x = x * 4
x /= 2 # Equivalent to x = x / 2
x %= 3 # Equivalent to x = x % 3
print("x:", x)
Example 3: Comparison Operators
x = 5
y = 3
print(x > y)
print(x < y)
print(x >= y)
print(x <= y)
print(x == y)
print(x != y)
Example 4: Logical Operators
x = True
y = False
print(x and y)
print(x or y)
print(not x)
Example 5: Conditional Expressions
x = 5
y = 3
max_value = x if x > y else y
print("Max value:", max_value)
Exercises
Exercise 1: Question: What are the main types of operators covered in this topic? Answer: The main types of operators covered in this topic are arithmetic operators, assignment operators, comparison operators, logical operators, and conditional expressions.
Exercise 2:
Question: What does the %
operator do in Python?
Answer: The %
operator calculates the remainder of a division operation.
Exercise 3:
Question: How can you perform an assignment operation and update the value of a variable in one line?
Answer: You can use assignment operators, such as +=
, -=
, *=
, /=
, and %=
to perform an operation and update the value of a variable in one line.
Exercise 4:
Question: What is the result of the expression x == y
if x
is equal to 5
and y
is equal to 3
?
Answer: The result of the expression x == y
will be False.
Last updated
Was this helpful?