Sunday 19 February 2017

Simple Python Programs (For concept building).

Level - Beginner

(I prefer Pycharm Community Edition 2016.2.1 and python version 2.7 for coding , Download links for both are mentioned below ).



For coding you can use Python IDLE or cmd also .


Consider this programs for easy understanding of syntax , try it on your own .

1] If/else/elif  ~
#if else

print "this is shivam hello folks!!"
age = input("please enter your age==>")

#if else with numbers
if age > 18:
    print "hello sir you are allowed in pub"
elif age == 18:
    print "dude show you're id card"
else:
    print "dude you are just an infant"
#lets do this if, else thing with strings
name = "john"
if name is "sam":
    print "hello sam"
elif name is "robert": #you can use is for matching or you can use ==    
    print "hello robert"
elif name == "john":
    
    print "hello john , welcome to Mugshot Lounge"
else :
    
    print "he didnt came !!"

#output
"""C:\Python27\python.exe C:/Users/Shivam/PycharmProjects/untitled1/ex1(if).py

this is shivam hello folks!!
please enter your age==>25
hello sir you are allowed in pub
hello john , welcome to Mugshot Lounge
Process finished with exit code 0
"""
2] FOR loop ~


print "hello guys in this program let's learn about for loop"
foods = ["chinease","thai","indian","desert"] #list
print foods[0]

print foods[1]

print foods[2]

print "printing one by one the elements of the list is hectic job"  

#lets do it in a simple way

for f in foods: #lets not use i always ;)    
    print f  #this will print all list elements
print "\n"
#more with the for loop
for f1 in foods[:2]: #stop at 2nd position (slicing)    
    print f1
    
    print len(f1) #the "len" function will print the length of each element of list

#output
"""C:\Python27\python.exe C:/Users/Shivam/PycharmProjects/untitled1/ex2(for).py
hello guys in this program let's learn about for loop
chinease
thai
indian
printing one by one the elements of the list is hectic job ,


chinease
thai
indian
desert

chinease
8
thai
4
Process finished with exit code 0
"""
3] RANGE function ~
#lets learn range function

for i in range(10): #lets use i this time    
    print i

print "\n"
#more thingss
for x in range(5):
    
    print "shivam is awesome"

print "\n"
for x1 in range(5,15): #including 5 excluding 15 ie. 5,6,7,...,14    
    print x1


#output
"""
C:\Python27\python.exe C:/Users/Shivam/PycharmProjects/untitled1/ex3(range).py
0
1
2
3

4
5
6
7
8
9

shivam is awesome
shivam is awesome
shivam is awesome
shivam is awesome
shivam is awesome

5
6
7
8
9

10
11
12
13
14
"""
4] WHILE loop ~
#while loop

print "in this program lets learn about while loop"
number = 6
while number < 10:
    
    print number  #as the condition is always gonna be true
    
                  #thus the loop will be infinite so adding another statement    
    number += 1   #incrementing the number by 1

#output
"""
C:\Python27\python.exe C:/Users/Shivam/PycharmProjects/untitled1/ex4(while).py
in this program lets learn about while loop

6
7
8
9
Process finished with exit code 0

"""
5] Comments ~
print "comments are very useful in documentation"

#this is a single line comment
#this is also a comment
"""documentation interlude
or
multiple line comment"""
print "shivam" + " is a good guy" #concatinating two strings
print  'shivam',9 #concatinating string nd number , use comma
#output
"""
C:\Python27\python.exe C:/Users/Shivam/PycharmProjects/untitled1/comments.py

comments are very useful in documentation
shivam is a good guy
shivam 9
Process finished with exit code 0
"""
6] Continue ~
numberstaken = [2, 5, 9, 16, 17]

# when continue will execute nothing will be printed after it , 
#it will go to start of the loop
print "numbers tht are not taken are :"
for n in range(1, 20):
    
    if n in numberstaken:
        continue        print n

#output
"""
C:\Python27\python.exe C:/Users/Shivam/PycharmProjects/untitled1/continue.py
numbers tht are not taken are :
1
3
4
6
7
8

10
11
12
13
14
15
18
19
Process finished with exit code 0
"""
7] Break ~
number = 24

for n in range(101): #including 100       
    if n is number:
   
        print n,"yess! we found our number"           
        break #break out of loop or stop executing       
    else:
   
        print n


#output
"""C:\Python27\python.exe C:/Users/Shivam/PycharmProjects/untitled1/continue_n_break.py
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

16

17
18
19
20
21
22
23
24 yess! we found our number
Process finished with exit code 0
"""
8] Functions ~
def hello():

    print "hi guyss this is a function"
def area_of_circle(radius):
    
    area = radius * 3.14        print area

hello()

area_of_circle(25)

#output
"""
C:\Python27\python.exe C:/Users/Shivam/PycharmProjects/untitled1/functions.py
hi guyss this is a function
78.5
Process finished with exit code 0
"""

No comments:

Post a Comment

Best Data Analytics Documentary you should watch now!!

"Data is the new Oil ? NO: Data is the new soil" -David Mccandless Do you know how much data we create every single...