Python chapter 1 What are the Operators & Control Structures/Statement in python
Python chapter 1 What are the Operators & Control Structures/Statement in python
Hello everyone! Welcome back..........
We are going to learn the python programming language, which is very useful
from the future point of view.
Installation of Python on windows/Linux.
- For Windows- Download python from- Download Python
- For Linux- Python is already installed on Linux.
- We will install an Anaconda navigator, you can use the different tools also like pycharm and vs code.
- Download and install anaconda for windows/Linux- www.anaconda.com
After this installation open anaconda navigator and select jupyter
notebook
Just click on python 3 as a new file
So let's get started.
Course content:
- Arithmetic Operators.
- Relational Operators.
- Logical Operators.
- User input and output
- Control structure.
What are the operators and expressions?
- A simple example of an expression is 2+3.
- Most statements (logical lines) that you write will contain an expression.
- An expression can be broken down into operators and operands.
- Operators are functions that do something and can be represented by symbols such as "+" or particular keywords.
- Operators require some data to operate on, and such data is called operands.
Arithmetic Operators:
- ( plus +)- Adds two objects.
- ( minus -)Gives the subtraction of one number from the other; If the first operand is absent, it is considered zero.
- ( multiply *)- Gives the multiplication of two numbers or can return the string multiple times.
- ( power **)- Returns x to the power of y.
- ( divide /)- divide x by y.
- ( floor division //)- Returns the integer parts of the quotient.
- ( modulo %)- Returns the remainder of the division.
Relational Operators:
- ( less than <)- Returns whether x is less than y. All comparison operators return True and False.
- Note - Capitalization of these names ( True and False ). Comparison can be chained arbitrarily.
- ( grater than >)- Returns whether x is more significant than y.
- ( less than or equal to <=)- Returns whether x is less than or equal to y.
- ( greater than or equal to >=)- Returns whether x is greater than or equal to y.
- ( equal to = = )- Compares if the objects are equal.
- ( not equal to !=)- Compares if the objects are not equal.
Logical Operators:
- not ( boolean not)- If x is True it returns False. If x is False it returns True.
- or ( boolean or)- If x is True, it returns True, else it returns the evaluation of y.
- and ( boolean and)- x and y return false, else it returns the evaluation of y.
This topic is vast, so be careful.
Control Statements
- Conditional Statements
- If statement
- If ------ else statement
- If ------ elif statement
- Looping Statements
- For loop
- While loop
- Loop Control Statements
- Break Statement
- Continue Statement
- Pass Statement
Conditional Statements:
- Decision-making is the anticipation of conditions occurring while executing the program and specifying actions taken according to the conditions.
- Decision structures evaluate multiple expressions which produce True or False as the outcome. We need to determine which action to take and which statements to execute if the outcome is True or False otherwise.
- The general form of a typical decision-making structure found in most of the programming languages is given here.
- Python programming language assumes any non-zero and non-null values as True, and if it is either zero or null, then it is assumed as False value.
Conditional Statements : " if "
- An if statement consists of a Boolean expression followed by one or more statements.
- It is similar to that of other languages. The if statement contains a logical expression using which data are compared, and a decision is made based on the comparison result.
- If the Boolean expression evaluates to True, then the block of a statement(s) inside the if statement is executed. If the Boolean expression evaluates to False, then the first code set after the end of the if statement(s) is executed.
Conditional Statements : " if " , "else"
- An if statement can be followed by an optional statement, which executes when the Boolean expression is False.
- An else statement can be combined with an if statement. An else statement contains the block of code that executes if the conditional expression in the if statement resolves to 0 or a False value.
- The else statement is an optional statement, and there could be only one else statement following if.
Conditional Statements : " if " , "elif"
- The elif statement allows one to check multiple expressions for True and execute a block of code as soon as one of the conditions evaluates to True.
- Similar to the else, the elif statement is optional. However, there can be at most one statement; there can be an arbitrary number of elif statements following an if.
- Core Python does not provide a switch or case statements like other languages, but we can use if...elif statements to simulate switch cases.
Looping Statement
- In general, statements are executed sequentially. The first statement in a program is executed first, followed by the second, and so on. There may be a situation when one needs to execute a block of code several times.
- Python Programming language provides various control structures that allow for more complicated execution paths.
- A loop statement allows us to execute a statement or group of statements multiple times.
Looping Statement: " While " and " Infinite"
- A while loop statement in Python programming language repeatedly executes a target statement as long as a given condition is True.
- Here, statement(s) may be a single statement, or a block of statements. The condition may be any expression, and True is any non-zero value.
- The loop iterates while the condition is True. When the condition becomes False, program control passes to the line immediately following the loop.
- In Python, all the statements indented by the same number of character spaces after the programming construct are considered part of a single block of code. Python uses indentation as its method of grouping statements.
- The critical point of the while loop is that the loop might not ever run. When the condition is tested and the result is False, the loop body will be skipped, and the first statement after the while loop will be executed.
- A loop becomes an infinite loop if a condition never becomes False. One must use caution when using while loops because of the possibility that a given condition never resolves to a False value.
- This results in a loop that never ends. Such a loop is called an infinite loop. An infinite loop might be helpful in client/server programming, where the server needs to run continuously so that client programs can communicate with it as and when required.
Looping Statement: " For "
- Executes a sequence of statements multiple times and abbreviates the code that manages the loop variable.
- It can iterate over the items of any sequence, such as a list or a string. If a sequence contains an expression list, it is evaluated first. Then, the first item in the sequence is assigned to the iterating variable iterating_var. Next, the statements block is executed.
- Each item in the list is assigned to iterating_var, and the statement(s) block is executed until the entire sequence is exhausted.
Nested Loops:
- One can use one or more loops inside any other for or while loop.
- Python programming language allows using one loop inside another loop.
Looping Control Statement: "break."
- Terminates the loop statement and transfers execution to the statement immediately following the loop.
- The most common use for a break is when some external condition is triggered, requiring a hasty exit from a loop.
- The break statement can be used in both whiles and for loops.
- If we are using nested loops, the break statement stops the execution of the innermost loop and starts executing the following line of code after the block.
Continous while loop:
- With indefinite iteration, the number of times the loop is executed isn't specified explicitly in advance.
- Instead, the designated block is executed repeatedly as long as some condition is met.
- In the example given, the while loop will continuously execute until the user enters 0.
- Here condition may be either True or any.
Looping Control Statement: "continue"
- Causes the loop to skip the remainder of its body and immediately retest its condition before reiterating.
- It returns the control to the beginning of the loop. The continue statement rejects all the remaining statements in the current loop iteration and moves the control back to the top of the loop.
- The continue statement can be used in both whiles and for loops.
Looping Control Statement: "pass."
- The pass statement in Python is used when a statement is required syntactically, but you do not want any command or code to execute. The pass statement is a null operation; nothing happens when it executes.
- The pass is also helpful in places where our code will eventually go but has not been written yet (e.g., in stubs, for example).
- Python supports have an else statement associated with a loop statement.
- If the else statement is used with a for loop, the else statement is executed when the loop has exhausted iterating the list.
- If the else statement is used with a while loop, the else statement is executed when the condition becomes false.
- If it encounters the break command in the loop, the other part will not be called.
- If it does not encounter the break command in the loop, the other part will be called.
Just do practice on all operators and control statements. If you face any
difficulty, then comment below.
We will meet in the next chapter.
See you soon.
Join the conversation