Learn the Fundamentals of Python Programming: Assigning and Using Variables, Data Types, and Basic Operations

Nicolai Søderberg
4 min readDec 21, 2022

--

Photo by AltumCode on Unsplash

Welcome to the world of Python programming! In this tutorial, we’ll be covering the basics of working with variables in Python.

Before we dive in, let’s define what a variable is. In programming, a variable is a named location in memory where we can store a value. This value can be any of Python’s built-in data types, such as integers, strings, or booleans.

Now, let’s look at how to assign values to variables in Python. Assigning a value to a variable is as simple as using the equal sign (=). For example:

x = 5
y = "Hello, World!"
z = True

In the code above, we’ve created three variables: x, y, and z. We’ve assigned the value 5 to x, the string “Hello, World!” to y, and the boolean value True to z. You can use the type() function to check the data type of a variable:

x = 5
print(type(x)) # Output: <class 'int'>

y = "hello"
print(type(y)) # Output: <class 'str'>

z = True
print(type(z)) # Output: <class 'bool'>

Once we’ve assigned values to variables, we can use them to store and manipulate data. For example, we can use variables to perform basic math operations:

x = 5
y = 3

result = x + y # result will be 8
result = x - y # result will be 2
result = x * y # result will be 15
result = x / y # result will be 1.6666666666666667

In the code above, we’ve used the variables x and y to perform addition, subtraction, multiplication, and division. We’ve then stored the result of these operations in the result variable.

It’s important to note that in Python, division always returns a float, even if the result is a whole number. For example, the division 5 / 2 will return the float 2.5, not the integer 2. If you want to perform integer division, you can use the // operator, like this:

result = x // y  # result will be 1

As you can see this is one of Pythons small hidden secrets, that can make ones code a bit similar and can be used in various cases.

In addition to performing math operations, we can also use comparison operators to compare values. Comparison operators are used to compare two values and return a boolean value of True or False. For example:

x = 5
y = 3

result = x == y # result will be False
result = x != y # result will be True
result = x > y # result will be True
result = x < y # result will be False
result = x >= y # result will be True
result = x <= y # result will be False

In the code above, we’ve used the comparison operators == (equal to), != (not equal to), > (greater than), < (less than), >= (greater than or equal to), and <= (less than or equal to) to compare the values of x and y.

Finally, we can use logical operators to combine comparisons. Logical operators are used to combine boolean values and return a single boolean value. The three logical operators in Python are and, or, and not. For example:

x = 5
y = 3

result = (x > y) and (x != y) # result will be True
result = (x > y) or (x != y) # result will be True
result = not (x > y) # result will be False

In the code above, we’ve used the and operator to combine the comparisons (x > y) and (x != y), the or operator to combine the comparisons (x > y) and (x != y), and the not operator to negate the comparison (x > y).

In this article, we learned about variables in Python and how to use them to store and manipulate data. We covered the different data types that Python supports, such as integers, strings, and booleans, and how to assign values to variables. We also learned how to perform basic math operations, use comparison operators to compare values, and use logical operators to combine comparisons.

By now, you should have a solid understanding of the fundamentals of working with variables in Python. With this foundation in place, you can move on to more advanced concepts and techniques as you continue your journey to becoming a proficient Python programmer. Don’t be afraid to experiment and try out different code snippets to get a feel for how variables work in practice. Happy coding!

--

--

Nicolai Søderberg

I am a Data Consultant/System Specialist with a degree in Quantum Physics from the University of Copenhagen