Day 3 : Conditional and looping statements
Hello guys,
Here we are, Day 3🤟
Some of you are beginner and some of you have basic knowledge and understanding of python or any programming language.
Each and every language have their own syntax.
Previously on Day 2, we learn about variables, keywords, datatypes and operators.
Let's dive into
Conditional Statemts :
Quick Note :
Python relies on indentation (whitespace at the beginning of a line) to define scope in the code. Other programming languages often use curly-brackets {} for this purpose.
1] if statement :
If condition is true then block will execute.
Syntax:
if <condition>:
<statement>
In the form shown above:
1. <condition> is an expression evaluated in Boolean context, as discussed in the section on Logical Operators in the Operators and Expressions in Python tutorial.
2. <statement> is a valid Python statement, if <expr>:
<statement> must be indented.
Example:
2] if-else statement
If condition is true then only if block will execute and else block will skipped
If condition is false then else block will execute and if block will skipped.
Syntax:
if <condition>:
<statement(s)>
else:
<statement(s)>Example:
1 >>> x = 20
2
3 >>> if x < 50:
4 ... print('first suite')
5 ... print('x is small')
6 ... else:
7 ... print('second suite')
8 ... print('x is large')
9 ...
10 first suite
11 x is small
3]elif statement:
This is if-elif statement,
In this if condition false then we can check for another condition if that condition is also get false then we can check for condition and so on at the end else block will execute if no conditions satisfied. In some language it is called as if-else ladder.
Here we check for multiple conditions.
Syntax
if <condition1>:
<statement(s)>
elif <condition2>:
<statement(s)>
elif <condition3>:
<statement(s)>
...
else:
<statement(s)>Example
>>> name = 'Joe'
>>> if name == 'Fred':
... print('Hello Fred')
... elif name == 'Xander':
... print('Hello Xander')
... elif name == 'Joe':
... print('Hello Joe')
... elif name == 'Arnold':
... print('Hello Arnold')
... else:
... print("I don't know who you are!")
...
Hello Joe
Looping statements:
1] While Loop
In this, loop will execute until condition is true.
Here expressions mean condition.
Syntax:
while expression:
statement(s)Example
count = 0
while (count < 3):
count = count+1
print("Hello") Output
Hello Hello Hello
2] for Loop
For Loop is different from other languages for loop.
I. E. for (i=0; i<n; i++).
Sequence may be list or tuple or set or string.
Syntax
for iterator_var in sequence:
statements(s)for iterator_var in range(number):
statements(s) Example
l= ["Welcome", "To", "Pyworld"]
for i in l:
print(i)Output
Welcome To Pyworld
3] Nested loop
Simply means loop within loops.
Syntax
for iterator_var in sequence:
for iterator_var in sequence:
statements(s)
statements(s)while expression:
while expression:
statement(s)
statement(s)Example
for i in range(1, 5):
for j in range(i):
print(i, end=' ')
print()
Output
1 2 2 3 3 3 4 4 4 4
Control Statemts
1] Continue Statement
It returns the control to the beginning of the loop.
Example
for letter in 'hello':
if letter == 'e' or letter == 'l':
continue
print('Current Letter :'+ letter) Output
Current Letter : h
Current Letter : o
2] Break Statement
It brings control out of the loopExample
for letter in 'hello': if letter == 'l': break print('Current Letter :'+ letter) Output
Current Letter : h
Current Letter : e
3] Pass Statement
We use pass statement to write empty loops. Pass is also used for empty control statement, function and classes.Example
for letter in 'hello': passprint('Last Letter :'+ letter) Output
Last Letter : o
Task
Quick note:
These tasks are for you.
Don't cheat or copy paste code. You don't learn anything from that.
Just be honest to yourself.
If you stuck somewhere just ask in group, other people will help you for sure.
Take challenge😉👍
Try to do this within 2 hours. 🤟
Easy
1] Even or odd number
2] Armstrong number
3] Positive negative or zero
Medium
1] Factorial of number
2] Reverse strings without using built-in function.
Hard
1] all prime numbers from 1 to 100 (without using function)
Pattern programs.
Contact here if you get any problem
8805271377 WhatsApp
Follow here ❤
@mr._mephisto_ Instagram
There will be no restrictions just feel free to learn.
Share and take one more step to share knowledge to others.
Believe in yourself 🤟 you are awesome.
Be safe, Be happy😁
Take care of yourself and your family
Of course watch movies and series🤟😉
And follow the guidelines of government
Comments
Post a Comment