Friday, 23 February 2024

Decision control statements

 Decision control statements: To know in detail click here

1. If Statement:

Syntax:
if expression: #execute your code

Example:1

x = 30
if(x > 20):
    print(" x is largest")



2. If else Statement:

Syntax:
if expression: #execute your code else: #execute your code

Example:2

m = 30
n = 20
if(m > n):
    print(" m is largest")
else:
    print(" n is largest")

3. Nested if:

if statement inside if statement is called as nested if statement.

Syntax:
if expression: #execute your code
    if expression:
        #execute your code
    else:      #execute your code


Example:3
a = int(input("Enter your number"))

if a > 10:
  print("Above ten,")
  if a > 20:
    print("and also above 20!")
  else:
    print("but not above 20.")

4. elif Statement:

elif - is a keyword used in Python replacement of else if to place another condition in the program. This is called a chained conditional.

Syntax:
if expression: #execute your code elif expression: #execute your code else: #execute your code



Example:4


x = int(input("Enter your number:"))
y = int(input("Enter your number:"))

if x > y:
  print("x is greater")
elif x == y:
  print("both x and y are equal")
else:
  print("y is greater")


BASIC LOOP in Python:

Python has two primitive loop commands:

  • while loops
  • for loops
1. while loop:
Execute the statement as long as the condition is true.

Syntax:

initialization of counter
while condition:
    statement or logic
    increment/ decrement counter

Example:5

i = 1
while i < 6:
  print(i)
  i += 1



The break Statement:

Example:6

i = 1
while i < 6:
  print(i)
  if i == 4:
    break
  i += 1




The continue Statement: 

Continue to the next iteration if the condition is true

Example:7

n = 0
while n < 6:
  n = n + 1
  if n == 3:
    continue
  print(n)



The else Statement

When the condition is no longer true at that time we can run the else statement

Example:8

i = 1
while i < 8:
  print(i)
  i += 1
else:
  print("i is no longer less than 8")


What is pass statement in Python?

To avoid the indentation error in Python we simply used pass in Python. now you doubt in mind why we get an indentation error, so let's check this code.



Example:1

x = 20
 
if x > 20:
    # write code your here
 
print('Hello all of you!')


output:

IndentationError: expected an indented block after 'if' statement

This is happen when the user does not know what code to write, So the user simply places a pass at that line. 

Sometimes, the pass is used when the user doesn’t want any code to execute. 

So users can simply place a pass where empty code is not allowed, 

like in loops, function definitions, class definitions, or in if statements. So using a pass statement user avoids this error.




Use of pass keyword in Function

Example:2

def addition():
  pass



Use of pass keyword in Python Class

Example:3

class studentClass:
  pass



Use of pass keyword in Conditional statement

Example:4

m = 10
n = 20
 
if(m<n):
  pass
else:
  print("n<m")



Use of pass keyword in Python Loop 

Example:5

list1 =['a1', 'b1', 'c1', 'd1']
 
for i in list1:
    if(i =='a1'):
        pass
    else:
        print(i)


2. for loop:
Execute the statement as long as the condition is true.

Syntax:

for x in range(10):
    # do something

The range() Function

To Iterate (loop) through a set of code a specified number of times, we can use the range() function,


Example:1

for x in range(6):
  print(x)
else:
  print("Finally finished!")




Example:2
The range() function defaults to 0 as a starting value, however, it is possible to specify the starting value by adding a parameter: range(2, 8), which means values from 2 to 6 (but not including 8):

for m in range(28):

  print(m)




Example:3

The range() function defaults to increment the sequence by 1, however it is

 possible to specify the increment value by adding a third parameter: range(3, 60, 3):

for x in range(3603):

  print(x)




Example:4


Note: 

The else block will not be executed if the for

 loop is stopped by the break keyword.


for m in range(8):

  if m == 4break

  print(m)

else:

  print("Finally finished!")




Python Collections (Arrays)

There are four collection data types in the Python

 programming language:

1.List:

- It is a collection which is ordered and changeable.  

- Allows duplicate members.

Example:

adj = ["red", "big", "tasty"]


fruits = ["apple", "banana", "cherry"]


for x in adj:

    for y in fruits:

        print(x,y)


2. Tuple:

- It is a collection which is ordered and

 unchangeable. 

- Allows duplicate members.

Example:

tuple2 = (15793)

print(tuple2)


3. Set :

- It is a collection which is unordered,

 unchangeable*, and unindexed. 

- No duplicate members.

- due to unordered you can not be sure in which

 order items appear in output.

Example:

set1 = {"abc"34True40"male"}

print(set1)


4. Dictionary 

- It is a collection which is ordered** and

 changeable. from 3.6 onwards unordered.

- No duplicate members.

Example:

thisdict1 = dict(name = "Joshi", age = 46, country = "India")

print(thisdict1)



Example:


thisdictstud = {

  "name""Ram",

  "age": 25,

  "class": "BE Comp"
}


print(thisdictstud["name"])