Control statements in Python:
If statement: The condition if evaluates to true the statement will get executed else the program will get terminate .As like other languages python just follows indentation and in its syntax involves no curly braces just it involve the conditions and statement to be executed.
if expression: //it should end with colon
statements
Example:
if 10>8:
print(“10 is greater than 8”)
print(“Program terminates as condition true”)
We can use if statement multiple number of times in a program.
Else statement:An else statement follows an if statement and evaluates if the condition given in if evaluates to false so the statement in else block is executed.
Elif statements: This is used as a shortcut statement for the if else if we want to give more conditions in a program and evaluate the results.
Boolean logic:The Boolean is used to check whether the result evaluates to true or false and also make comparsion whether the number is equal to other number or any condition but also provides us in form of logic in python which enables us to use operator and ,or and not.
And operator:The and operator takes both arguments and evaluates to true if only both of the arguments are true else the condition is considered false.
if(1==1)and (2+2>3):
print(“True”)
else
print(“false”)
Or operator:The and operator takes both arguments and evaluates to true if only one of the arguments is true and if both false is considered false.
if(1==1)and (2+1>3):
print(“True”)
else
print(“false”)
Not operator:The not operator takes one argument and if is true inverts to false and false invert to true.
not(1==1)
False
not(1>7)
True
Lists: The other type provided by python is lists and it is used to store indexed value.It is created using square brackets and comma used to separate.
Example:
message=[“Hello”,”world”,”1”]
print(message[0])
print(message[1])
print(message[2])
Empty list:It is created with just a pair of braces.
empty_list=[]
print(empty_list)
Nested list:List can include other lists also
things=[“string”,0,[1,2,3],4]
print(things[1])
print(things[2][2])
Indexing of lists:It means the index of the number which we are using in the print statement and it is used accordingly.
Kommentare