Welcome to Day 2 of the 50 Days of Python: Zero to Hero series! Yesterday, we set the stage by getting Python installed and running your first ever python script (maybe not first for some of you). Today, we’re going to be diving deeper into two fundamental building blocks of Python: Variables and Data Types.
Understanding these is crucial because they form the foundations of everything you’ll do with Python. Whether that would be manipulating data, building ML models are orchestrating ETL pipelines in data engineering.
What are Variables?
I’d describe variables to be like containers that hold data. They allow us to store, retrieve and manipulate information within our programs. In Python, creating a variable is simple… You just assign a value to a name using the = operator.
Example:
# Assigning values to variables
name = "python"
version = 1.0
currently_learning = True
Data Types: What Can You Store in Variables?
Python provides several built-in data types to classify the kind of data a variable can hold. The most common ones are:
Numbers
Interger (int): Whole numbers, e.g. 5, 10, -20.
Float (float): Decimal numbers, e.g. 2.1, -0.5.
Complex (complex): Numbers with real and imaginary parts, e.g. 2 + 3x.
Example:
age = 31 # Interger
price - 12.99 # Float
complex_num = 2+3x # Complex
Text (Strings)
Strings (str) are a collection of characters enclosed in single ‘ ‘ or double “ “ quotes.
Example:
message = "Welcome to Day 2!"
Boolean
Boolean (bool) are your True or False values, used in logical operations.
Example:
is_active = True
is_student = False
Collections
Collections are used to store multiple items in a single variable. Key examples of these are:
List (list): Ordered and mutable.
Tuple (tuble): Ordered but immutable.
Dictionary (dict): Key-Value pairs.
Set (set): unordered, unique values.
Example:
numbers = [1,2,3,4,5] # List
coordinates = (10, 20) # Tuple
person = {"name":"Jonathon", "age":"31"} # Dictionary
unique_items = {1, 2, 3} # Set
Dynamic Typing in Python
Python is dynamically types, which means that you don’t need to explicityl declare a variables type. The type is inferred from the value you assign:
Example:
x = 10 # Innitally declared as an integer
x = "Hello" # Now is delcared as a string
This makes Python flexible but requires attention when working with larger projects to maintain clarity within your code.
Best Practices for Variables
Firstly, use descriptive names. This makes sure that there is clarity on what a variable is for:
good_example = “username”
bad_example = “x”
Secondly, follow the standard snake casing convention. Which means use underscores instead of spaces e.g. total_cost, first_name.
Thirdly, don’t start variables with special characters or numbers. e.g. 1total, ?pricing.
Common Pitfalls and Gotchas
Forgetting that python is case sensitive.
figure = 5 print(Figure) # Error
Mixing data types without conversion
number = 25 string = "Python print(number + string) # Error
Hands on Exercise
Assign the following Values to variables in your environment:
Your name.
Your age.
Your favourite programming language.
Whether you love coding.
Print each variables along with its type:
name = "Your Name" age = 31 favourite_language = "Python" loves_coding = true print("Name:", name, type(name)) print("Age:", age, type(age)) #... and so on
Fix the type error in the code below:
price = 49.99 quantity = "3" total = price * quantity print(total)
Next Up: Day 3 - Operators & Expressions
Tomorrow, we’ll cover what operators are, when and how to use them. As well as expressions and operator precedence within an expression.
So stay tuned as we delve deeper and deeper into Python. If you want to discuss today’s topic leave a comment below or connect on Substack!