Open gedit and type
print "hello world"
Save the file as filename.py
In your terminal, type
user:~$ python filename.py
print "python is cool"
a = input()
b = 5
c = "hello"
d = a + b
b = b + 1
a += 1
print d, b
Try them!! Take around 7 and a half minutes :)
Conditional Statements
if <condition 1>:
statement 1
statement 2
....
Pay special attention to indentation and the colon (:) symbol
Conditional Statements
Enhancing the if-conditions
if <condition 1>:
statement 1
statement 2
....
elif <condition 2>:
statement 3
statement 4
else:
statement 5
....
Pay special attention to indentation and the colon (:) symbol
I give you a point in the graph.
You tell me which quadrant it lies in. Can you?
Sample Input (Take x as input first then y as input):
-2
3
Sample Output (Print the quadrant number):
2
Don't go to the next slide before you try this question !!
Hope you tried it yourself first :)
x = input()
y = input()
if x > 0 and y > 0:
print 1
elif x < 0 and y > 0:
print 2
elif x < 0 and y < 0:
print 3
elif x > 0 and y < 0:
print 4
else:
print "Not in any quadrant"
Dont forget the indentation and colon (:) where required
Looping statements
while <condition>:
statement 1
statement 2
....
Pay special attention to indentation and the colon (:) symbol
Give me even numbers from 1 to 100
Sample Input:
{no input}
Sample Output:
2
4
6
8
...
...
98
100
We will try this for another 7 and a half minutes :P
You need to submit a Physics Assignment. What steps will you take to complete it?
Functions help us stand on the shoulders of giants!
import math
pi = math.pi
a = math.sin(pi/2)
print a
This is a list. It is bound by square brackets []
[1, 2, "hello", [3, 4]]
This is a tuple. It is bound by round brackets ()
(1, 2, "hello", [3, 4])
Once created, contents of tuples cannot be changed. For a list, we can modify.
One last thing before you become a Ninja Python Programmer
for loop
number = input()
ls = [1, 2, 3, 4]
for multiples in ls:
print number, "x", multiples, "=", multiples * number
Pay special attention to indentation and the colon (:)
Let's head over to building the game.... Click here