Python for Absolute Beginners: Variables and Data Types

Kasim Ali
6 min readMar 16, 2023

Are you curious about the fascinating world of Python data types? Look no further! In this exciting medium post, we’ll dive into the various data types available in Python and even explore the concept of variables. But before we jump in, let’s get a solid understanding of what data types are. Simply put, they’re values — or containers for values — that can be manipulated in various ways. Today, we’ll explore some of the most important data types in Python, including:

  • Strings
  • Integers
  • Floats

You can access course Replit here and the course notes here.

If you prefer to follow along on YouTube you can do so here.

*If you are using the course Replit to follow along, make sure to fork the content so you can play along and practice your new skills.

**The course notes will be useful for revision and to brush up on your newly found skills.

Printing and Data Types.

The first piece of Python you will use is the Print function. This useful function prints a value to the console. There are several ways we can pass a given value to this function which you will learn about in this post.

A value can be something like:

4

‘4’

‘Hello’

variable_name

4.4

Each of these values has its data type too. We can find the data type by using the type function. Head on over to the Replit so you can follow along.

Let's begin by using the print function. To use this function all we need to do is the following:

print('Hello World') #prints to console log
print(4.4)
print(3)
print('4')

You have just learned about all three of the data types you will learn about today. However, there are 4 print statements, why?

The answer is in the detail, so let's take a look at what each of these data types is.

A string is a value that is wrapped in ‘_’ or “_”.

An integer is a number with no decimal point.

A float is a number with a decimal point.

Given that the last print statement contains a number wrapped in single quotation marks. What data type do you think this is?

Use the following code below as a guide so that you can find out in the Replit:

print(type('')) #write anything inside of the single quotation marks
print(type(1)) #replace 1 with any number you want
print(type(0.0)) #replace 0.0 with any decimal number you want
print(type('44')) #write any number inside the single quotation marks

What did you find out about your number wrapped in single quotation marks? That is right, it is actually a string. This is significant because of what we can do with data types. Using Python we use the ‘+’ differently depending on whether we have a string or a numeric value like an integer or float.

Use the following code to use the ‘+’ so that we can discover more about how Python deals with different data types:

#Make sure to do these one by one so you can catch the error
print(4+4) #returns int
print(4+4.0) #returns float
print(4+'4') #will this work?
print('fourty'+4) #will this work?
print('fourty'+'four') #what happens here?

You will have noticed that print statements 3 and 4 present errors but the 5th does not.

That is because the + operator works when both values are numeric or both values are strings but not with each other.

Two important distinctions to make:

When using the + operator with strings, it is called concatenation.

When using the + operator with numeric values, it is called addition.

Something else you will have noticed is that the 2nd statement returns 8.0 which is a float and not an integer. This becomes more significant later.

Go back to the Replit and use the print() and type() functions so that you can find the data types for each line of code.

Variables and Storing Data.

So far, so good. You have learned about data types, how to print values, how to do math using the + operator and how to check data types.

Variables are a great way to store values or data so that you can reuse them over and over again in your projects.

Here is how we declare a variable in Python:

first_name = 'Alexander'
last_name = 'Smith'

It is that easy! What is great about variables is that we can print using the variable name like so:

first_name = 'Samantha'
print(first_name)
print(type(first_name))

Why don’t you go ahead and declare two variables, print them and then find out the type for each variable too? Head on over to the Replit to do this.

Bringing it all together!

You are now going to use concatenation to put together a string that reads:

‘Hello! My name is John Smith and I am 30 years old’.

I would declare three variables, one for the first name, one for the last name and one for the age. Whether you save the age as a string or integer is up to you. Though, I would suggest not storing it as a float since no one really writes their age to a decimal point.

You might even need to consider the use of a string with a space so that you can correctly separate your first name and last name variables. Here is an example:

print(first_name + ' ' + last_name) #prints with a space
print(first_name + last_name) #prints with no space

If this is at all confusing, use the Replit to experiment with this so that you can gain an intuition of what this means. Simply declare the variables and print them like the example above. You will realise very quickly, why the space is needed.

Take a look at the Replit again.

Some of you might have realised that strings with apostrophes will cause errors when we use the single quote method of wrapping strings.

We can overcome this in several ways, here are some examples.

print('Don't touch that') #Python will not interpret this correctly
print("Don\'t touch that") #We can use backslash to escape the character
print('''
"Don't touch that"
'''')
#We can use triple single quotes for multi line comments
#We can also use double and single quotes inside multi line comments

Get some practice in the Replit by printing the following correctly

“That is Mike’s cat!”

“I don’t think we have met before.”

“Did you say you can or you can’t”

Use whichever method you prefer.

Input function.

So far, your code has been very static. Here is where things get interesting and we can start to ask the user for information to use in our application.

We can prompt the user for information and store it in a variable like so:

first_name = input('What is your first name?')

The user will be asked to type in their first name in the console which is on the right-hand side by default on the Replit IDE.

Note that that variable will hold a string by default, this will not cause any issues as of right now but we will need to deal with it in later lessons.

The variable has now been declared as whatever the user inputs after they are asked ‘What is your first name?’.

Head over to the Replit to find out more.

F Strings Assignment.

F strings are another way we can potentially eliminate difficulty with printing variables alongside strings and integers.

If I wanted to print my name, age and hobbies with and without an F string I would have to do the following:

#Without variables
print('Hello, My name is Alexander Lionheart and I am 56 years old. I like to eat burgers on the weekend')

#with variables
print('Hello, My name is ' + first_name + ' ' + last_name + ' and I am ' + age + ' years old. I like to ' + hobbies)

#with F string
print(f'Hello, My name is {first_name} {last_name} and I am {age}. I like to {hobby}')

For your final assignment, I want you to prompt the user for their first name, last name, age and hobby.

Save each of these inputs as a separate variable and use an f string to print it all out so that the web page introduces the user.

See how much easier it was using an F string?

That is everything for this lesson. In the next lesson, we will learn some new functions, mathematics and build our very own BMI calculator!

If you liked this, please consider following me on Medium and subscribing to my YouTube channel.

--

--