Code With Me (python)— Day 2: Understanding Variables and Data Types in Python

zaqee
3 min readOct 3, 2023

Welcome back to Day 2 of our “Code With Me” series! Yesterday, we explored the print function and how it allows us to display information in Python programs. Today, we'll dive deeper into the world of programming by discussing variables and data types.

What Are Variables?

In programming, variables are like containers that hold data. You can think of them as labels for values, making it easier to manipulate and work with information in your code. Python is dynamically typed, which means you don’t need to explicitly declare a variable’s data type. Let’s start with some basics.

Declaring and Assigning Variables

To declare a variable in Python, you simply choose a name and use the = operator to assign a value to it. Here's an example:

age = 30
name = "Alice"

In this code, we’ve created two variables: age and name. age holds the integer value 30, while name holds the string "Alice."

Variable Naming Rules

When naming variables in Python, you need to follow a few rules:

  • Variable names must start with a letter (a-z, A-Z) or an underscore (_).
  • Variable names can contain letters, numbers, and underscores.
  • Variable names are case-sensitive (e.g., myVar and myvar are different variables).

Data Types in Python

Python supports various data types, and it automatically assigns a data type to a variable based on the value assigned to it. Some common data types include:

  • int: Integer values (e.g., 42)
  • float: Floating-point or decimal values (e.g., 3.14)
  • str: Strings, which are sequences of characters (e.g., “Hello, World!”)
  • bool: Boolean values, which can be either True or False
  • list: Lists, which are ordered collections of values (e.g., [1, 2, 3])
  • tuple: Tuples, which are similar to lists but immutable (e.g., (1, 2, 3))
  • dict: Dictionaries, which store key-value pairs (e.g., {“name”: “Alice”, “age”: 30})

You can check the data type of a variable using the type() function:

age = 30
print(type(age)) # This will output <class 'int'>

Type Conversion

Sometimes, you may need to convert one data type to another. Python provides built-in functions for this purpose. For example:

num_str = "42"
num_int = int(num_str) # Convert the string to an integer

Concatenating Strings and Other Operations

You can perform operations on variables, depending on their data types. For instance, you can concatenate strings, perform mathematical operations with numbers, and more

first_name = "John"
last_name = "Doe"
full_name = first_name + " " + last_name # Concatenate strings
age = 30
double_age = age * 2 # Multiply two integers

Wrapping Up

In Day 2, we’ve covered the basics of variables and data types in Python. Understanding how to declare variables, work with different data types, and perform operations on them is crucial for building complex programs.

Stay tuned for Day 3, where we’ll explore control structures like conditional statements and loops, which will allow you to make decisions and repeat actions in your code. Happy coding!

“Feel free to practice what you’ve learned on Replit, a user-friendly online coding platform where you can write and share your Python code with ease. It’s a fantastic place to collaborate on projects and showcase your work to the world!”

--

--