Variables and Data Types in Python | Python for beginners

Hey, perks! Welcome to this tutorial on variables and data types in Python. We’ve already gone through the introduction to python in the previous post. Don’t forget to test your skills in the quiz section at the end of this post.

Let’s get started!

Data Types in Python | What are Data Types in Python?

Basically, a data type is a simple attribute of data that tells the interpreter or compiler how the programmer wants to use the data.

Don’t worry if this definition looks complicated. You’ll understand it soon.

Now we’re ready to learn a little bit about data types. It is important to understand the basic datatypes so there are four main data types in Python.

There are a few more but we’re just going to talk about these ones right now.

1. Integers

Integer can be any number, including 0. It is a set of positive numbers, negative numbers and 0.

Let’s see how we can work with integers in Python:

int 9
int -6
print(7 + 8)

Okay so we’re done with integers and now let’s learn about another data type in Python.

2. Strings

A string is nothing but plain text. Let’s say I want to print a text saying “HTML is not a programming language”, I can do this using a simple print statement and quotation marks.

print("HTML is not a programming language")

Concatenate Strings in Python

Strings can be added using the “+” symbol.

print("Hi there!" + "Welcome to Python")
print("My friend can hack NASA, " + "using HTML :P")

That’s simple, right?

3. Booleans

Booleans contain only two values; True and False.

Remember that you cannot write “true”. It should be written only “True” with “T” as a capital letter.

4. Floats

Floats are basically decimal numbers. I can also add floats in Python which will output a float.

print(2.0 + 45.7)

OUTPUT

47.7

So we’re done learning about floats in Python. Now let’s learn about Variables in Python.

What are variables in Python?

An easy way to understand variables is that a variable is a box, and we add values to it.

In other words, they can be called a placeholder that holds values that can be changed.

variables in python

Now let’s say I want to assign a variable say x, and give it a value, say 45 (integer). Now I want to print the value of x.

x = 45
print(x)

I hope you are finding it easier to learn these concepts.

Similarly, you can assign different values to a variable like strings, floats, booleans etc.

Working with variables

We can add variables like this:

x = 5
y = 10
print(x + y)
#the output will be 15

In the code above, we assigned 2 different values to 2 different variables x and y. The third line of this code printed the output by adding both the values of variables.

A variable name cannot start with a number. You can use underscores, letters in your variable, and numbers in between.

Let’s look at another example, where the values of the variables are going to be strings.

variable_one = "I am learning python"
variable_two = " at"
variable_three = " IQ of Machines"
#the output will be "I am learning python at IQ of Machines" 

Now I want to clear something out. Let’s say you assign a value of 45 (which is an integer) to a variable. The second variable is assigned a value of 60.0 (float). When I add them together, the sum will be a float.

Casting

Casting helps you to specify the data type of a variable.

x = str(5)    # x will be '5'
y = int(5)    # y will be 5
z = float(5)  # z will be 5.0

Getting the Type

The data type of a variable can be identified using the type() function.

x = 5
y = "John"
print(type(x))
print(type(y))

Case-Sensitive

Remember that, a variable name is case-sensitive. For example, in the code below, “x” is not the same as “X”.

x = 4
X = "Sally"
#X will not overwrite "x"

Multiple Variables

Do you know that you can assign multiple variables in a single line?

x, y, z = "IQ", "of", "Machines"
print(x)
print(y)
print(z)

I hope you understood the concept of variables in Python. We also learned data types in Python.

Getting the user Input

Getting input from the user is easy in Python. You can do this by just typing the input() function. Let’s see an example of how to do so.

x = input()
print(x)

In the above code, we declared a variable ‘x’ and assigned the value as the user input. In the second line, we used the print statement to get the input printed out.

Input Statement

Now, what if you want to ask for specific input from the user; like a username. You will need an input statement for that.

We use parenthesis to produce the prompt message. Below is an example:

username = input("Please enter your username: ")
print(username)

You can also add a string to the input this way:

username = input("Please enter your username: ")
print("Hello, " username + "Welcome!")

So it’s time to test what you’ve learned.

Take a Quiz!

0%

Create a variable "age" and assign the value 42 to it:

Correct! Wrong!

Which of these is a valid variable name in Python?

A variable name never starts with a number.
Show hint
Correct! Wrong!

What is the output of this code?

Correct! Wrong!

Pick the correct answer for the variable "y" to output 12:

Correct! Wrong!

Does Python have a command for declaring a variable

Correct! Wrong!

Declaring a variable is nothing but just to declare a variable name without assigning a value to it. Whereas assigning a variable means to add a value to the variable.

Which of these functions can be used to get the data type of a variable?

Correct! Wrong!

Now you might be clear with data types and variables, getting the user input in python now. Please feel free to comment below your thoughts or questions.

Default image
Riddhi
Hi! I’m the creator of iqofmachines.com, a platform where I help beginners learn how to code. I’m here to share my favorite tools, resources, and strategies to help you learn the right tech skills faster.

Leave a Reply