Learn about variables, data types, and operators in Python. Practical examples and applications.
Working with data in Python begins with understanding the basic elements of the language, such as variables, data types, and operators. Variables allow for storing and manipulating data, data types define the kind of information being stored, and operators enable performing operations on this data.
In Python, variables are created automatically when assigning a value. We don’t need to declare their types in advance, which makes the language very flexible.
x = 5
y = "Hello, World!"
z = 3.14
When naming variables, it’s important to follow a few rules:
age = 30
Age = 25
_age = 40
Python supports several basic data types that are essential for working with various kinds of data.
int: integers
age = 25
float: floating-point numbers
pi = 3.14
complex: complex numbers
complex_number = 2 + 3j
str: strings
name = "John Doe"
bool: boolean values (True/False)
is_active = True
list: lists
numbers = [1, 2, 3, 4, 5]
tuple: tuples
coordinates = (10.0, 20.0)
range: ranges
range_of_numbers = range(1, 10)
dict: dictionaries
person = {
"name": "John",
"age": 30,
"city": "New York"
}
Operators in Python allow for performing various operations on variables and values. Here are some of the most important types of operators:
They allow for performing basic mathematical operations.
a = 10
b = 5
# Addition
print(a + b) # Output: 15
# Subtraction
print(a - b) # Output: 5
# Multiplication
print(a * b) # Output: 50
# Division
print(a / b) # Output: 2.0
# Modulo (remainder)
print(a % b) # Output: 0
# Exponentiation
print(a ** b) # Output: 100000
They are used to compare values.
a = 10
b = 5
print(a == b) # Output: False
print(a != b) # Output: True
print(a > b) # Output: True
print(a < b) # Output: False
print(a >= b) # Output: True
print(a <= b) # Output: False
They are used for logical operations.
a = True
b = False
print(a and b) # Output: False
print(a or b) # Output: True
print(not a) # Output: False
They are used to assign values to variables.
a = 10
a += 5 # Same as a = a + 5
print(a) # Output: 15
a -= 3 # Same as a = a - 3
print(a) # Output: 12
a *= 2 # Same as a = a * 2
print(a) # Output: 24
a /= 4 # Same as a = a / 4
print(a) # Output: 6.0
Understanding variables, data types, and operators in Python is crucial for working efficiently with data. Python offers a simple and intuitive syntax that makes performing complex data operations easy. I hope this article helped you understand the basic elements of Python and encouraged you to learn more. I encourage you to experiment with the code and discover how Python can simplify your work with data.
Other interesting articles:
Prefer to read in Polish? No problem!
That’s all on this topic. Analyze in peace!
Did you like this article 🙂?
Share it on Social Media 📱
>>> You can share it on LinkedIn and show that you learn something new every day.
>>> You can throw it on Facebook – and perhaps help a friend of yours who is looking for this.
>>> And remember to bookmark this page, you never know if it won’t come handy in in the future.
You prefer to watch 📺 – no problem
>>> Subscribe and watch my English channel on YouTube.