Author: SUNNY BHAVEEN CHANDRA
For more information -
[1] Python notes- https://c17hawke.github.io/Python/
[2] Python YouTube Playlist- https://youtube.com/playlist?list=PLrdaCCBhU_hnxIzB7EJlY-pfYOMGRycAK
User input, Data-type casting/conversions ๐ยถ
User input in Python. ๐ฅยถ
In Python, you can use the input()
function to take input from the user.
The input()
function takes an optional string argument which is shown as a prompt to the user.
After taking input, the input()
function returns the value entered by the user as a str
(i.e. string).
For example:
name = "SUNNY BHAVEEN CHANDRA"
lecture = input("Name of the lecture?: ")
print(f"Hello, everyone! My name is: {name}")
print(f"Welcome to the lecture of: {lecture}")
Name of the lecture?: SUNNY BHAVEEN CHANDRA Hello, everyone! My name is: SUNNY BHAVEEN CHANDRA Welcome to the lecture of: SUNNY BHAVEEN CHANDRA
print(type(lecture))
<class 'str'>
The main use of user input in Python is to allow users to interact with the program and provide information in runtime so that the program can use to perform specific tasks.
For example, a program might ask the user for their name and then use that information to personalize a greeting message as show above.
User input can also be used to control the flow of a program.
NOTE: You can use input function without using prompts but it is not recommended as the user will be confused what to enter.
x = input() # without prompt
print(x)
12 12
# let's provide a prompt to the user to give some context-
x = input("Enter your age: ")
print(x)
Enter your age: 24 24
print(f"You entered: {x}")
print(f"type of data entered: {type(x)}")
You entered: 24 type of data entered: <class 'str'>
N = 5
print(f"Age after {n} years: {age + N}") # throws error
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In[6], line 1 ----> 1 print(f"Age after 5 years: {x + 5}") # throws error TypeError: can only concatenate str (not "int") to str
The above example leads us to the topic of type casting in python or conversion of one data type to another.
Handling various data-types and it's conversion ๐งยถ
The method of converting a Python variable's data type into a certain data type in order to perform the required operation by users. There are two types of it in Python: Implicit Type Conversion and Explicit Type Conversion.
Implicit conversions aka Data-type conversion ๐ยถ
In this method, Python automatically converts the data type into another data type. Users donโt have to involve in this process. For example:
# int + float = float
x = 24 # int
y = 37.4 # float
z = x + y
z
61.4
print(f" x + y = z ")
print(f"{x} + {y} = {z}")
# mathematically allowed
x + y = z 24 + 37.4 = 61.4
print(f"type of x: {type(x)}")
print(f"type of y: {type(y)}")
print(f"type of z: {type(z)}")
type of x: <class 'int'> type of y: <class 'float'> type of z: <class 'float'>
# int * float = float
x = 24
y = 37.4
z = x * y
print(f" x * y = z ")
print(f"{x} * {y} = {z}")
print(f"type of x: {type(x)}")
print(f"type of y: {type(y)}")
print(f"type of z: {type(z)}")
# mathematically allowed
x * y = z 24 * 37.4 = 897.5999999999999 type of x: <class 'int'> type of y: <class 'float'> type of z: <class 'float'>
# int / int = float
x = 24
y = 37
z = x / y
print(f" x / y = z ")
print(f"{x} / {y} = {z}")
print(f"type of x: {type(x)}")
print(f"type of y: {type(y)}")
print(f"type of z: {type(z)}")
# mathematically allowed
# simpel representation
24/37
x / y = z 24 / 37 = 0.6486486486486487 type of x: <class 'int'> type of y: <class 'int'> type of z: <class 'float'>
0.6486486486486487
# int + complex = complex
x = 24
y = 37.4 + 2j
z = x + y
print(f" x + y = z ")
print(f"{x} + {y} = {z}")
print(f"type of x: {type(x)}")
print(f"type of y: {type(y)}")
print(f"type of z: {type(z)}")
# mathematically allowed
24 + 37.4 + 2j
x + y = z 24 + (37.4+2j) = (61.4+2j) type of x: <class 'int'> type of y: <class 'complex'> type of z: <class 'complex'>
(61.4+2j)
# float + complex = complex
x = 24.4
y = 37.4 + 2j
z = x + y
print(f" x + y = z ")
print(f"{x} + {y} = {z}")
print(f"type of x: {type(x)}")
print(f"type of y: {type(y)}")
print(f"type of z: {type(z)}")
# mathematically allowed
24.4 + 37.4 + 2j
x + y = z 24.4 + (37.4+2j) = (61.8+2j) type of x: <class 'float'> type of y: <class 'complex'> type of z: <class 'complex'>
(61.8+2j)
Explicit conversions aka Data-type casting ๐๏ธยถ
In this method, Python needs user involvement to convert the variable data type into the required data type. Mainly, type casting can be done with these data type functions: int()
, float()
, and str()
and more as per requirement. For example:
age = input("Age: ?")
N = 5 # int
print(f"BEFORE: type of age variable: {type(age)}")
age = int(age)
print(f"AFTER: type of age variable: {type(age)}")
print(f"type of N variable: {type(N)}")
print(f"Age of the user after {N} years will be: {age + N}")
Age: ?24 BEFORE: type of age variable: <class 'str'> AFTER: type of age variable: <class 'int'> type of N variable: <class 'int'> Age of the user after 5 years will be: 29
The need for type-casting arises when you want to ensure that your data is compatible with specific functions or operations (e.g. converting a variable to a string before concatenating it to another string). Type casting can also be useful when you want to make sure that a variable contains the right type of data.
# str + float
x = "24.4"
y = 37.4
z = x + y
print(f" x + y = z ")
print(f"{x} + {y} = {z}")
print(f"type of x: {type(x)}")
print(f"type of y: {type(y)}")
print(f"type of z: {type(z)}")
# mathematically not allowed
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In[15], line 4 2 x = "24.4" 3 y = 37.4 ----> 4 z = x + y 5 print(f" x + y = z ") 6 print(f"{x} + {y} = {z}") TypeError: can only concatenate str (not "float") to str
# str + float
x = float("24.4")
y = 37.4
z = x + y
print(f" x + y = z ")
print(f"{x} + {y} = {z}")
print(f"type of x: {type(x)}")
print(f"type of y: {type(y)}")
print(f"type of z: {type(z)}")
# Now mathematically allowed
float("24.4") + 37.4
x + y = z 24.4 + 37.4 = 61.8 type of x: <class 'float'> type of y: <class 'float'> type of z: <class 'float'>
61.8
# str + float
x = int(float("24.4"))
y = 37.4
z = x + y
print(" x + y = z ")
print(f"{x} + {y} = {z}")
print(f"type of x: {type(x)}")
print(f"type of y: {type(y)}")
print(f"type of z: {type(z)}")
x + y = z 24 + 37.4 = 61.4 type of x: <class 'int'> type of y: <class 'float'> type of z: <class 'float'>