Featured Post

SQL Interview Success: Unlocking the Top 5 Frequently Asked Queries

Image
 Here are the five top commonly asked SQL queries in the interviews. These you can expect in Data Analyst, or, Data Engineer interviews. Top SQL Queries for Interviews 01. Joins The commonly asked question pertains to providing two tables, determining the number of rows that will return on various join types, and the resultant. Table1 -------- id ---- 1 1 2 3 Table2 -------- id ---- 1 3 1 NULL Output ------- Inner join --------------- 5 rows will return The result will be: =============== 1  1 1   1 1   1 1    1 3    3 02. Substring and Concat Here, we need to write an SQL query to make the upper case of the first letter and the small case of the remaining letter. Table1 ------ ename ===== raJu venKat kRIshna Solution: ========== SELECT CONCAT(UPPER(SUBSTRING(name, 1, 1)), LOWER(SUBSTRING(name, 2))) AS capitalized_name FROM Table1; 03. Case statement SQL Query ========= SELECT Code1, Code2,      CASE         WHEN Code1 = 'A' AND Code2 = 'AA' THEN "A" | "A

How to Check If Statement Multiple Conditions in Python and Ensure Tidy Code

Here're examples for Python multiple if conditions (statements). These are useful for interviews and projects. Many programmers confuse to write IF logic in Python. Below examples useful for your quick reference.


Python IF Statements Multiple Conditions Examples

Multiple IF Conditions

  1. IF, IF
  2. IF 'ELSE'
  3. IF 'or'
  4. IF 'and'
  5. Nested IF
  6. IF 'continue'
  7. IF 'break'

In Python, the decision-making logic you can write with IF condition. You can write multiple IF conditions (Single way decision). At the same time, you can write IF and ELSE conditions (Two-way decision).


Multiple IF conditions the best example.

def main(): 
       celsius = float(input("What is the Celsius temperature? ")) 
       fahrenheit = 9/5 * celsius + 32 
       print("The temperature is", fahrenheit, "degrees Fahrenheit.")
 # Print warnings for extreme temps 
     if fahrenheit > 90: 
          print("It's really hot out there. Be careful!") 
     if fahrenheit < 30: 
          print("Brrrrr. Be sure to dress warmly!")
 

Note:  Writing IF conditions like this we call it a Single way of Decision. 

IF-Else condition the best example.


def main(): 
      print("This program finds the real solutions to a quadratic\n") 
      a = float(input("Enter coefficient a: ")) 
      b = float(input("Enter coefficient b: ")) 
      c = float(input("Enter coefficient c: ")) 
      discrim = b * b - 4 * a * c 
     if discrim < 0: 
          print("\nThe equation has no real roots!") 
     else: 
         discRoot = math.sqrt(b * b - 4 * a * c) 
         root1 = (-b + discRoot) / (2 * a) 
         root2 = (-b - discRoot) / (2 * a) 
         print("\n The solutions are:", root1, root2

Note: Writing IF conditions like this we call it Two way of Decision.

 

Nested IF condition the best example.

if condition:
	if condition:
		statements
	else:
		statements
else:
statements


IF Continue condition the best example.


Notes: 'Continue' just continue the next iteration of the loop.

>>> for num in range(2, 10):
...     if num % 2 == 0:
...         print "Found an even number", num
...         continue
...     print "Found a number", num
Found an even number 2
....
 

IF break conditions the best example.


>>> for n in range(2, 10):
...     for x in range(2, n):
...         if n % x == 0:
...             print n, 'equals', x, '*', n/x
...             break
...     else:
...         # loop fell through without finding a factor
...         print n, 'is a prime number'
...
 

IF True Best Example.


flag = True
if flag:
    print("Welcome")
    print("To")
    print("BeginnersBook.com")

IF 'and' the Best Example.

if((a>b and a>c) and (a != b and a != c)): 
    print(a, " is the largest") 
elif((b>a and b>c) and (b != a and b != c)): 
    print(b, " is the largest") 
elif((c>a and c>b) and (c != a and c != b)): 
    print(c, " is the largest") 
else: 
    print("entered numbers are equal"
 

Related Posts

Comments

Popular posts from this blog

How to Fix datetime Import Error in Python Quickly

Explained Ideal Structure of Python Class

How to Check Kafka Available Brokers