CONDITIONAL STATEMENTS IN PYTHON
What are the logical operators in Python?
Logical operators in Python are used for conditional statements are true or false. Logical operators in Python are AND, OR and NOT. For logical operators following condition are applied.
What are membership operators in Python?
Membership operators test for membership in a sequence such as lists, strings or tuples. There are two membership operators that are used in Python. (in, not in). It gives the result based on the variable present in specified sequence or string
How do you write an if-else statement ?
An if = else statement has an if statement that runs if the condition is True and an else clause that runs if the condition is False: a = 200 b = 33 if b > a: print("b is greater than a") else: print("b is not greater than a") Ouput: b is not greater than a
How do you write a simple if statement?
An if statement consists of a conditional test on one line and a statement or block of statements that run if the test returns True: a = 33 b = 200 if b > a: print("b is greater than a")
How do you write an if-elif-else statement?
An if-elif-else block has an if statement, a series of test conditions if the first test fails, and an else bloc that runs if all tests fail. (Note that an if-elif block does not require an else block) a = 200 b = 33 if b > a: print("b is greater than a") elif a == b: print("a and b are equal") else: print("a is greater than b") Output: a is greater than b
What do the while True loops do?
Clearly, True will never be false, or we're all in very big trouble. Thus, while True: initiates an infinite loop that will theoretically run forever. An example of an infinite loop: while True: print("Hello!")
How to iterate through items in a dictionary?
Code: user = {"First Name":"John", "Last Name": "Doe"} for key, value in user.items(): print(key) print(value) Output: First Name John Last Name Doe
What are the comparison operators in Python?
Comparison Operators In Python compares the values on either side of the operand and determines the relation between them. It is also referred to as relational operators. Various comparison operators in python are ( ==, != , <>, >,<=, etc.)
What is a Conditional Statement in Python?
Conditional statement in Python evaluates the code to see if it meets the specified conditions. The conditions are evaluated and processed as true or false. If this is found to be true, the program is run as needed.
What does the Ennumerate() function do in Python?
Enumerate() method adds a counter to an iterable and returns it in a form of enumerating object. This enumerated object can then be used directly for loops or converted into a list of tuples using the list() method.
What are identity operators in Python?
Identity Operators in Python are used to compare the memory location of two objects. The two identity operators used in Python are (is, is not). Identity Operators in Python are used to compare the memory location of two objects. The two identity operators used in Python are (is, is not). Operator is: It returns true if two variables point the same object and false otherwise Operator is not: It returns false if two variables point the same object and true otherwise Following operands are in decreasing order of precedence.
How do you write short hand if and if-else statements?
If you have only one statement to execute, you can put it on the same line as the if statement. a = 33 b = 200 if a > b: print("a is greater than b") or a = 2 b = 330 print("A") if a > b else print("B")
What is Python List Slicing?
In Python, list slicing is a common practice and it is the most used technique for programmers to solve efficient problems. Consider a python list, In-order to access a range of elements in a list, you need to slice a list. One way to do this is to use the simple slicing operator i.e. colon(:) With this operator, one can specify where to start the slicing, where to end, and specify the step. List slicing returns a new list from the existing list. Lst[ Initial : End : IndexJump ]
What is the difference between == and is operator in Python?
The Equality operator (==) compares the values of both the operands and checks for value equality. Whereas the 'is' operator checks whether both the operands refer to the same object or not (present in the same memory location).
What is the break statement in while loops?
The break statement ends a loop when a certain condition occurs.
What is the continue keyword in while loops?
The continue statement instructs a loop to continue to the next iteration. Any code that follows the continue statement is not executed. Unlike a break statement, a continue statement does not completely halt a loop. You can use a continue statement in Python to skip over part of a loop when a condition is met.
What is the elif keyword in Python?
The elif keyword is pythons way of saying "if the previous conditions were not true, then try this condition". a = 33 b = 33 if b > a: print("b is greater than a") elif a == b: print("a and b are equal") Output: and an b are equal
What is the else keyword in Python?
The else keyword catches anything which isn't caught by the preceding conditions.
What is the input function in Python?
The input() function pauses a program and waits for the user to enter data. It has one parameter input(prompt) Note that all data enters via the input statement is converted into a string. Numerical data needs to be converted to the appropriate type before you can work with it.
What does for _ in range(10): do?
When you are not interested in some values returned by a function we use underscore in place of variable name . Basically it means you are not interested in how many times the loop is run till now just that it should run some specific number of times overall. Code: for _ in range(3): print("Hello") Outcome: Hello Hello Hello
What is a for loop?
With the for loop we can execute a set of statements, once for each item in a sequence (that is either a list, a tuple, a dictionary, a set, or a string). * The for loop does not require an indexing variable to set beforehand. The basic structure of a for loop: for [item] in [sequence]: # Run code or for [item] in range(start_integer, end_integer, step_length_integer): #Run code
What is a while loop in Python?
With the while loop we can execute a set of statements as long as a condition is true. The while loop requires relevant variables to be ready, in this example we need to define an indexing variable, i, which we set to 1.
What does the :: (double colon) operator mean in Python?
[::1] means: Start at the beginning, end when it ends, walk in steps of 1 (which is the default, so you don't even need to write it). [::- 1] means: Start at the end (the minus does that for you), end when nothing's left and walk backwards by 1.
Give an example of a conditional operator used with an if statement.
a = 200 b = 33 c = 500 if a > b and c > a: print("Both conditions are True") Outcome: Both conditions are true.
What is the pass keyword?
if statements cannot be empty, but if you for some reason have an if statement with no content, put in the pass statement to avoid getting an error. Code: a = 33 b = 200 if b > a: pass Output: Nothing