Monday, November 30, 2015

Python Control Statements

Break Statement:



break
Example:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
#!/usr/bin/python

for letter in 'Python':     # First Example
   if letter == 'h':
      break
   print 'Current Letter :', letter
  
var = 10                    # Second Example
while var > 0:              
   print 'Current variable value :', var
   var = var -1
   if var == 5:
      break

print "Good bye!"
Continue:
continue
Example:
1
2
3
4
5
6
7
str1="hello"
for i in str1: # 'hello'
    if i=='l':
  continue
  print "Hai"
    else:
        print i
Pass:
Syntax:
pass
Example:
1
2
3
4
5
6
7
str1="hello"
for i in str1:
    if i == 'e':
        pass
        print "Whats up"
    else:
        print i

Python Conditional Statements

1. Basic IF Statement :
if condition:
    Block 
    statements 

Example:
1
2
3
4
5
6
if True:
    print "its True Block"
if 10:
    print "its True Block"
if 1<2:
    print "Yes its True 1 is lesser then 2"
2. IF.. Else... Statement
if condition:
    True Block
else:
    False Block
Example:
a=1
b=2
if a<b:
    print "its b is big"
else:
    print "its a is big"

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
if True:
    print "its True Block"
 
if not 23:
    print "Yes i am not 23"
else:
    print "Yes i am 23"

if None:
    print "Not none"
else:
    print "I am NOne"

3. IF.. Elif...elif... Statement

if condition1:
    Cond1 Block 
elif Condition2:
    Cond2 Block
elif Condition3:
    Cond3 Block 
 .
 .
4. IF.. Elif...elif...else... Statement

if condition1:
    Cond1 Block 
elif Condition2:
    Cond2 Block
elif Condition3:
    Cond3 Block 
 .
 .
else:
    Else Block
Example:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
a=int(raw_input("Enter a value"))
b=int(raw_input("Enter b value"))
c=int(raw_input("Enter c value"))

if a<b and b<c:
    print "C is big"
elif a>b and a>c:
    print "A is big"
else:
    print "B is big"
5. Nested IF.. Elese Statement
if Condition1:
    if Condition2:
     Condition2 True Block 
 else:
     Condition2 False Block 
else:
    if Condition3:
     Condition3 True Block 
 else:
     Condition3 False Block
Example:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
if a<b:
   if b<c:
       print "C is big"
   else:
       print "B is big"
else:
   if a>c:
       print "A is Big"
   else:
       print "C is big"

Python Looping Statements

Python For Loop:

           If a sequence contains an expression list, it is evaluated first. Then, the first item 
in the sequence is assigned to the iterating variable iterating_var. Next, the statements 
block is executed. Each item in the list is assigned to iterating_var, and the statement(s) 
block is executed until the entire sequence is exhausted.



for iterating_var in sequence:
   statements(s)
Example:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
n=input("Enter the value:")
for i in range(n):
    print "i:",i

str1=raw_input("Enter the String:")
for i in str1:
    print "i:",i
    
n=input("Enter the value:")
l=[]
for i in range(n):
 en=raw_input("Enter the String:")
 l.append(en)
Prime Numbers:
1
2
3
4
5
6
for num in range(10,20): #[10,11,12,12,....19]
    for i in range(2,num):
        if num%i == 0:
            break
    else:#else for
        print "prim Num",num
Fibonac Series :
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
def fib_gen(n):
#fib 0,1,1,2,3,5...
    l=[0,1]
    for i in range(2,n):
        fib=l[i-1]+l[i-2]
        l.append(fib)
    return l

n=input("Enter Number")
l=fib_gen(n)
print "Fib List:",l

While Statements :
            Statement(s) may be a single statement or a block of statements. The condition may be any expression, and true is any non-zero value. The loop iterates while the condition is true.

When the condition becomes false, program control passes to the line immediately following the loop.
while expression:
   statement(s)
Example:
1
2
3
4
a=1
while a<10:
   print a
   a=a+1
Prime Number using while:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
a=10
n=200
b=1
while a< n:
 c=2
 while c<a-1:
  if a%c==0:
   break
  c=c+1 
 else:
     print "Prime:",a
     
 if b==10:
  print ">>",b
  break
 else:
     b+=1
 a=a+1#a+=1

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
c=1
l=[]
p="="*30
n=raw_input("Enter number how many std ")
while c<int(n):
    name=raw_input("Enter Name")
    age=raw_input("Enter Age")
    l.append({'Name':name,"Age":age})
    c=c+1
    
print p
print "        Std Details      "
print p
for i in l:
    print i['Name']
    print i['Age']