1# CHECKING IF INT
2variable='3'
3try:
4 int(variable)
5 # this will execute if it is integer
6 print('You typed an int')
7except ValueError:
8 # otherwise this will execute
9 print('You did not type an int')
10
11# CHECKING IF FLOAT
12variable='3'
13try:
14 float(variable)
15 print('You typed a float')
16except ValueError:
17 print('You did not type a float')
1# Use the function int() to turn a string into an integer
2string = '123'
3integer = int(string)
4integer
5# Output:
6# 123
1# this is a string
2a = "12345"
3# use int() to convert to integer
4b = int(a)
5
6# if string cannot be converted to integer,
7a = "This cannot be converted to an integer"
8b = int(a) # the interpreter raises ValueError
1#INTEGERS
2# Use the class int() to turn a string into a integer
3s = "120"
4s = int(s)
5print(s+1)
6#121
7#FLOATS
8# Use the class float() to turn a string into a float
9s="2.5"
10s = float(s)
11print(s*2)
12#5.0