Variables and Data Types
Variables and Data Types
Output
Output
Case sensitive
In Python, variable names (as with all identifiers) are case sensitive. Therefore,
the following are two different variables:
firstname = "Cristiano"
Firstname = "Melon Eusk"
print(firstname)
print(Firstname)
Outputting will give us two different values because they are 2 different
variables.
In the above code you can see that there are 3 different variables created with
3 different values.
• abc is storing a String data type.
• _abcnum is storing an Integer data type.
• abc123 is storing a Float data type.
Output
This tells us that firstName variable has a string stored in it and age variable
has an int stored in it.
Note – We can't do arithmetic operations of numbers with strings i.e. we
can't add a string to any number. Have a look at the example below:
var1 = "It's a String"
var2 = 5
print(var1 + var2) # It will give an
# error as we can't add string to any number.
Output
Note – We can add (concatenate) two or more strings and the strings will be
concatenated to return another string. Here’s the example showing that:
var1 = "My Name is "
var2 = "xyz"
var3 = var1 + var2 + " & I am a Good Boy."
print(var1 + var2)
print(var3)
Output
Typecasting
Typecasting is the way to change one data type of any data or variable to
another datatype, i.e., it changes the data type of any variable to some other
data type.
We know it is a bit confusing but let me tell you in a simple manner. Suppose
there is a string "34" Note: String is not integer since it is enclosed in double-
quotes) and as we know we cannot add this to an integer number let us say 6.
But to do so we can typecast this string to int data type and then we can add
34+6 to get the output as 40. Have a look at the program below:
Example
# Typecasting in Python:
abc = 5
abc2 = '45'
abc3 = 55.95
xyz = 5.0
abc4 = int(abc2)
print(abc + abc4)
print(abc + int(abc2))
print(float(abc) + xyz)
Output
There are many functions to convert one data type into another type:
• str() – this function allows us to convert some other data type into a
string.
• int() – this function allows us to convert some other data type into an
integer. For example, int("34") returns 34 which is of type integer (int)
• float() – this function allows us to convert some other data type into
floating-point number i.e. a number with decimals.
Input Function
This function allows the user to receive input from the keyboard into the
program as a string.
input() function always takes input as a string i.e. if we ask the user to take a
number as input even then it will take it as a string and we will have to
typecast it into another data type as per the use case.
If you enter 45 when input() is called, you will get "45" as a string.
Example
# Input Function in Python:
print("Enter your name : ")
name = input() # It will take input from user
print("Your Name is", name) # It will show the name
xyz = input("Enter your age : ")
print("Your age is", xyz)
Output
Example
Ask 2 numbers from user and print the sum of those numbers.
Let us solve this question in different style showing you common mistakes.
Ans 1 (Does not give expected answer)
a = input("Enter number 1 = ")
b = input("Enter number 2 = ")
print(a + b)
This solution does not work because, whenever we take input from user, it’s
data type will always be a string. And we know when we add 2 strings, the
result will always be a concatenation of 2 strings.
Ans 2
a = input("Enter number 1 = ")
b = input("Enter number 2 = ")
c = int(a)
d = int(b)
print(c + d)
This will give us proper solution because we converted our both strings into
integers and stored them in c and d.
Ans 3
a = int(input("Enter number 1 = "))
b = int(input("Enter number 2 = "))
print(a + b)
This solution will also work because we directly converting the string at the
same line as input.
Output
‘%s’ is used to inject strings similarly ‘%d’ for integers, ‘%f’ for floating-point
values, ‘%b’ for binary format.
Try these examples below:
pi = 3.14563
print("The value of pi is: %f" % pi)
print("The value of pi up to 2 decimal is: %.2f" % pi)
pi = 3.14563
print(f"The value of pi is: {pi}")
Output