Now, you know how to print something on a screen. So, let's learn how to take input from the user.
name = raw_input("What is your name >>>")
For Python 3:
name = input("What is your name >>>")
print "Enter your name" x = raw_input() y = raw_input("age >>>") #age>>> will be printed before input print "Your name is",x,"and","and your age is",y,
Your name is xyz and and your age is 20
'raw_input()' is used to take input from user. We can also write something inside raw_input() to make it appear before the input, as in the previous example (raw_input("age >>>")).
Let's see some examples.
print "Enter your wish" wish = raw_input() print "May your wish come true!"
I want to be greatest coder ever
May your wish come true!
Anything given to raw_input is returned as a string. So, if we give an integer like 5, we will get a string i.e. '5' ( string ) and not 5 ( int ).
Now, let's learn to take integer input from the user.
x = input("Enter an integer >>>") print "You have entered",x
You have entered 12
For Python 3:
x = int(input("Enter an integer >>>")) print ("You have entered",x)
You have entered 12
Python gives us a different function for taking integer inputs. It is input().
'input()' is used to take integer values.
Let's see what happens if we pass something other than an integer to 'input()'
x = input()
NameError: name 'xyz' is not defined
Since 'xyz' is not an integer, that's why we are getting this error.
What does input() and raw_input() do?
x = input(">>>") print x
This code is similar to x = 10.
In input(), we have given here 10. Now, think that input() gives us 10. So, x = input() will be similar to x = 10 after the input is given.
Similar is the case with raw_input().
x = raw_input().
Suppose, we gave xyz. Then, this is similar to x = 'xyz'.
Let's think that if we give some integer, like 7, to raw_input(), then it would be like
x = '7'
x = 7 (not). And we know ('7') is a string.
How to take float (decimals)?
x = float(raw_input()) print x print type(x)
It's like, we are taking some value and converting it into float. And we are doing exactly the same.
We entered 12.32 to raw_input which will return '12.32' ( a string ) and not 12.32 ( a decimal ). Thus, the expression float(raw_input()) will become float('12.32'). In this way, after we entered 12.32, we are converting it into float.
Now, let's try this.
This is type casting. 7.4 is a float (decimal). So by writing int(7.4), we are converting it into integer and thus it gets converted to 7.
This is very useful in some situations, where you want to convert a variable in the middle of your program.
We just have to write the type in which we want to convert before that value. eg- type(value)
We can also take integers in this way:
x = int(raw_input()) print x print type(x)
Let your computer do some maths for you.
import math print math.sin(30) print math.cos(10) print math.pow(2,3)
import math This will include Python's inbuilt directory 'math'. It contains many mathematical functions for our use. 'import' is a keyword used to import any available directory.
math.sin() computes sine of given angle.
math.cos() computes cosine of given angle.
math.pow(a,b) computes a raised to the power of b (ab).
We have imported 'math' and these functions are inside it, so 'math.sin()' can be understood as 'sin()' from 'math' directory.
Checkout python official documentation for more functions available in math.
Knowledge is of no values unless you put it into practice.