You are on page 1of 17

PYTHON 2.

x
With Linux :-D

Python 3 is not Python 2.x


Python 3 is completely different compared with python 3. Most
of the syntaxes are changed in the new version. DO NOT watch
python 3 tutorials!!!

What we learn is Python 2.x. All the linux distros use Python 2.x.
To get the python version of your pc, open the terminal and enter
$

python

Python is used in 2 ways


Interactive mode The interactive mode uses a Python shell
where users can work with the python interpreter interactively by
giving one command at a time.

Command line mode This is non interactive and used to


compile and run more than a single line of code.

>> well learn Command-line mode. Read the given notes and learn
how to use the interactive mode.

Write codes to get this!


$ python code.py
Hello
$ python code.py
$ python code.py

Enter input : (1+5)*10

Enter input : hihi

60

hihi
$ python code.py

$ python code.py
Enter input : (1+5)*10
(1+5)*10

Enter input : [x*2 for x in


range(0,10,2)]

[0,4,8,12,16]

print My name is %s and I am %d years old.


My GPA is %f. :D

print String, 1, 2.345, Hello %d % 5

The Temperature converter (C to F )


Write a program to Convert a given
Celsius value to Fahrenheit.
$ python ctof.py
Enter Celsius value : 25
Output 77

Use this equation : C x 9/5 + 32 = F

Important Operations and Expressions

Arithmetic - % , ** , //
Comparison - <>
Assignment - **= , //=
Logical and, or, not
Membership in , not in
Identify is , is not

Conditional Statements
if(bla bla):
Do this

elif(bla bla bla):


Do that

else:
Do something

Write a code to determine whether a given


year is a Leap year or not

If the year is evenly divisible by 400, then its definitely a


leap year.

If the year is divisible by 4 and not divisible by 100 then


its a leap year.

Otherwise its not a leap year.

LOOPS
While loop
while expression:
statement(s)
For loop
for <iterating_variable> in <sequence>
statement(s)

Try these programs using while loops


Print all the even numbers between
1 to 20 (including 20) , print DONE
when the loop ends.

**
*****
*****
*****
*****
*****

***
****

*****

x=0
y=0
while x<5:
y=0;
while y<x+1:
print "*",
y+=1
print ""
x += 1

The syntax range is useful when moving to for loops and Lists
>> range(5)
[0,1,2,3,4]

>> range(10,0,1) #What would happen here?

>> range(2,6)
[2,3,4,5]

>> range(10,0,-1) #What would happen here?

>> range(0,10,2)
[0,2,4,6,8]

for loops
Write a code to Take a String by input,
then print the letters of the input one by one.
Eg: Input : Indika
Output : I n d i k a

Students = [Bimal, Binal, Udesh, Anu , Pachira,]


Write a program to access each item in a sequence by its index.
The list should be a user input.
Eg: Input : Bimal, Binal
Output:
Index 0 : Bimal
Index 1 : Binal

Utopian tree!
The Utopian tree goes through 2 cycles of growth every
year. The first growth cycle of the tree is during the
monsoon season when it doubles in height. The second
growth cycle is during the summer when it increases in
height by 1 meter. If a new Utopian tree sapling of height
1 meter is planted just before the onset of the monsoon
season, can you find the height of the tree after N cycles?
samples :
Input : 3 Output : 6
Input : 4 Output : 7
Source : www.hackerrank.com

num = input("Number of cycles : ")


N=0
ans=1
while N != num:
if(N%2 == 0):
ans *= 2
else:
ans += 1
N += 1

print "Height of the tree after %d cycles : %d" %


(num,ans)

Got a problem? GOOGLE it , may be you are not the only


one in this world who got that prob.
Use efficient resources. Stackoverflow.com is the best
Do not blindly learn python 3 :P
Good sites to learn python :
http://www.tutorialspoint.com/python
Official python docs on docs.python.org/2/tutorial

Good programs to work on:


www.projecteuler.com
www.hackerrank.com
Got a problem installing python on windows?
Go to www.repl.it Awesome online python Compiler with the Interactive support.

You might also like