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

OFFSET in Computer Science

OFFSET in Computer Science In the post, We will understand what is the meaning of OFFSET in computer science with an example. In computer science offset comes picture at may place like packing a data structure, encoding the data etc in some standard protocol so that other parties can deserialise it and understand the data. We will understand its important how does it work with example. suppose we have a data structure for a student like { name:string[20] roll: int[4] course: string[25] batch: int[4] } We want to serialise below student data  { name:"Amit Gupta" roll: 1234 course:Computer Science" batch: 2018 } Our protocol follow the rule that we will insert a value and if space will remain empty we will fill it with $ for example length of "Amit Gupta" is 10 but standard size is 20 so will fill 10 $ after "Amit Gupta". So serialisation of above data will be Amit Gupta$$$$$$$$$$1234Computer Sc...

Variable In C

Variable Like any other programming language in C programming language, we use a variable to store value, a value is any data like a number a or a character. In this post, we will learn about datatype of C programming language. Type Of Variable In C In this section, we will discuss how to store the following type of variable in C How to store an integer in C? How to store a float in C? How to store a double in C? How to store a long in C? How to store a character in C? As we already discussed that variable used to store data so that we can use it of other computation, for example, suppose we want to calculate the sum of two number then we have to store these two numbers first only after that we can use these two stored value to compute the sum. The entity that used to store a value called variable. What does it mean is suppose we tell a variable to store a number whose value is 5, the variable will place this number 5 in memory and we can variable in...

Clique in Graph

Clique in Graph In this tutorial, we will learn about the clique in the graph theory. Clique In graph theory, a clique is a subset of vertices of an undirected graph such that every two distinct vertices in the clique are adjacent. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Supplementary angles

Supplementary angles Supplementary angles  are two angles with a sum of  180 ^\circ 1 8 0 ∘ 180, degree . A common case is when they lie on the same side of a straight line.