Skip to main content

Python Control Flow

Python Control Flow

if Statements
The if statement is used for conditional execution
x = 5
if x < 0:
    print('x is Negative')
elif x == 0:
    print('x is Zero')
elif x > 0:
    print('x is Positive')
else:
    print('x is not a number')
The while statement
The while statement is used for repeated execution as long as an expression is true
x = 0
while x < 6:
    print(x)
    x = x + 1:
for Statements
In Python’s for statement iterates over the items of any sequence (a list or a string)
fruits = ["apple", "orange", "banana", "cherry"]
for fruit in fruits:
    print(fruit)
The range() Function
If you do need to iterate over a sequence of numbers, the built-in function range() comes in handy. It generates arithmetic progressions
for i in range(5):
    print(i)
Output
0
1
2
3
4
More example on range
range(5, 10)
   5, 6, 7, 8, 9

range(0, 10, 3)
   0, 3, 6, 9

range(-10, -100, -30)
  -10, -40, -70
break and continue Statements
The break statement, breaks out of the innermost enclosing for or while loop
The continue statement, continues with the next iteration of the loop:
Break
for x in "string":
    if x == 'i':
        break
    else:
        print('inside loop')
Continue
for x in "string":
    if x == 'i':
        continue
    else:
        print('inside loop')
pass Statements
The pass statement does nothing. It can be used when a statement is required syntactically but the program requires no action
while True:
    pass
Define function
Functions are used to utilize code in more than one place in a program
def add(x, y):
    return x + y

print (add(1 + 2))
print (add(1.5 + 2.5))
print(add("Python is ", "awesome"))

Comments

Popular posts from this blog

Time, Speed and Distance

Time, Speed And Distance  In this post, We will learn about time, speed and distance. Important Formula Distance = Time x Speed Time = Distance/Speed Speed = Distance/time Important Units Distance: meter (m), kilometre (km) Time: Hour (h), second (s) speed: meter/second (m/s), kilometre/hour  (km/h) Important Conversion 1 km = 1000 meter 1 h = 3600 s 1 km/h = (5/18) m/s 1 m/s = (18/5) km/h Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 

Joint Entrance Exam (JEE): Probability Syllabus

JEE: Probability Syllabus Probability  is a very interesting and different topic of mathematics which doesn't require your previous knowledge of any other chapters other than permutation and combination.  Following topics are the part of Jee syllabus Addition and multiplication rules of probability Addition and multiplication rules of probability Conditional Probability Bayes Theorem Independence of events Computation of probability of events using permutations and combinations Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Haryana Public Service Commission: HPSC 2018

Haryana Public Service Commission: HPSC 2018 Company Name         :  Haryana Public Service Commission: HPSC 2018 Recruitment Post Name                       :   Civil Judge State                            :  Haryana Salary                           :  Rs   27,700-44,770 Number of Vacancy   :  107 Eligibility                     :  Bachelor's Degree in Law Last Date to Apply     :  30-09-2018 Application Details    :  Notification Apply Online              :  Apply Here Government Jobs In  Haryana Please write comments if you f...

Securities and Exchange Board of India Recruitment 2018

Securities and Exchange Board of India  Company Name SEBI Post Name Assistant Manager State India Salary RS 28150-55600 Number of Vacancy 120 Eligibility Graduation Last Date 07-10-2018 Application Details Job Notification Apply Online Job Application Securities and Exchange Board of India (SEBI) Recruitment 2018    Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.