☞To take input from the user, we use input() function
☞Observe the following example
☞Example :
a=input() print(a) print(type(a))
5 5 <class 'str'>
Note : 1. input() takes input as a string. #Here, 5 is not a number it is a string.
☞To take input from the user, we will use input() function and then convert it into integer using int() function.
☞Example :
a=input("Enter a number : ") x=int(a) OR x=int(input("Enter a number : ")) print("The answer is ",x)
Enter a number : 45 The answer is 45
a=input("Enter a number : ")
x=int(a)
By writing these two lines, it will take input as a string and stores in variable 'a'.
Then in the next line, it will convert value of variable 'a' into integer and stores it in variable 'x'.
x=int(input("Enter a number : "))
This line will take a input as a string but not store in any variable, as you can see input() function is in int() function.
Value will get convert into integer and stores it in variable 'x'.
#You can use either way to take input.
☞To take input from the user, we will use input() function and then convert it into floating point using float() function.
☞Example :
a=input("Enter a number : ") x=float(a) OR x=float(input("Enter a number : ")) print("The answer is ",x)
Enter a number : 45.567 The answer is 45.567
As same as above, only use float instead of int.
☞To take input from the user, we will use input() function and then convert it into complex number using complex() function.
☞Complex number must be of the form : a + bj
☞Example :
a=input("Enter a number : ") x=complex(a) OR x=complex(input("Enter a number : ")) print("The answer is ",x)
Enter a number : 45 + 3j The answer is (45+3j)
As same as above.
#Output will come alongwith parenthesis if real part is non-zero.
☞If you want to print real part and imaginary part of complex number separately, we use real and imag attributes.
☞Syntax : <complex object>.real for real part or <complex object>.imag for imaginary part.
☞Example :
a=complex(input("Enter complex number : ")) print(a) print("Real part =",a.real) print("Imaginary part =",a.imag) print("--------------------") b=complex(input("Enter complex number : ")) print(b) print("Real part =",b.real) print("Imaginary part =",b.imag)
Enter complex number : 4+5j (4+5j) Real part = 4.0 Imaginary part = 5.0 -------------------- Enter complex number : 0+34j 34j Real part = 0.0 Imaginary part = 34.0