
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.
Variables in Python
Creating and Assigning Values
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
Naming Variables
When naming variables, it’s important to follow a few rules:
- Variable names must start with a letter or an underscore (_).
- They can contain letters, digits, and underscores.
- Case sensitivity matters (variable x and X are two different variables).
age = 30
Age = 25
_age = 40
Data Types in Python
Python supports several basic data types that are essential for working with various kinds of data.
Numeric Types
int: integers
age = 25
float: floating-point numbers
pi = 3.14
complex: complex numbers
complex_number = 2 + 3j
Text Type
str: strings
name = "John Doe"
Boolean Type
bool: boolean values (True/False)
is_active = True
Sequence Types
list: lists
numbers = [1, 2, 3, 4, 5]
tuple: tuples
coordinates = (10.0, 20.0)
range: ranges
range_of_numbers = range(1, 10)
Mapping Type
dict: dictionaries
person = {
"name": "John",
"age": 30,
"city": "New York"
}
Operators in Python
Operators in Python allow for performing various operations on variables and values. Here are some of the most important types of operators:
Arithmetic 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
Comparison Operators
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
Logical Operators
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
Assignment Operators
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
Checklist of Basic Operations in Python
- Creating Variables: Assigning values to variables.
- Naming Variables: Rules for creating variable names.
- Data Types: int, float, str, bool, list, tuple, dict.
- Arithmetic Operators: +, -, *, /, %, **.
- Comparison Operators: ==, !=, >, <, >=, <=.
- Logical Operators: and, or, not.
- Assignment Operators: =, +=, -=, *=, /=.
Conclusion
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:
- Data scientist vs data analyst – interview with Magda Kostrzewska
- Interview with Jan Tymiński: The Role of DevOps in Today’s IT World
- Advanced SQL Queries: Techniques and Examples
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.