Sunday, 20 February 2022

Python#02

EX 4: Using IF 

Now imagine you are asked to find right alliance 👫 by your parents just like in TV Programme KALYANAMALAI. Choose a bride/bridegroom based the traits like qualification and salary. You want to choose one who is earning around 1 lac and  better qualification. How python will simulate this situation and decide, let us code.

Now let us formulate the problem with variables qual and salary with values qual = UG, PG, Ph.D. (Rank 1,2,3 respectively), salary = 20k,40k,50k, 100k, 150k, 200k respectively
we have to pick the right bride/bridegroom.

#qual = [0,1,2] # 0 -ug, pg = 1, Ph.D = 3

#salary = (20,30,40,50,75,100,120) #

qual = input("Enter candidates qualification 0 -UG, 1- PG, 2 - Ph.D :\n ")

sal = input("Salary of the candidate :\n")

# Check select better qualified Ph.D, and salary with 100|120k

if int(qual) >= 2:
if int(sal) >= 100:
print("Candidate is chosen for marriage alliance")
else:
print("Candidate is not chosen since salary is not sufficient")
else:
print("Candidate is not chosen since Not having enough qualification")

 Run :


Enter candidates qualification 0 -UG, 1- PG, 2 - Ph.D :
1
Salary of the candidate :
120
Candidate is not chosen since Not having enough qualification

Process finished with exit code 0
In the previous scenario, if we have list  of bride/bridegrooms to be checked for alliance, how to run through the list and identify, let us deep dive in python coding.

EX 5: Using IF..Else..

qual = (0, 1, 2)  # 0 -ug, pg = 1, Ph.D = 3

salary = (20, 30, 40, 50, 75, 100, 120) # 20 - 20k

# c = qual, salary
candidates = [[2, 200], [1, 120], [2, 80], [2, 120], [1, 80]]

for r in candidates:

if r[0] >= 2:

if r[1] >= 100:

print("Candidate qual = %2d salary = %6.2f is chosen for alliance" % (r[0], r[1]))

else:
print("Candidate qual = %2d salary = %6.2f is not chosen for alliance" % (r[0], r[1]))

else:
print("Candidate qual = %2d salary = %6.2f is not chosen for alliance" % (r[0], r[1]))

Result:

Candidate qual =  2  salary = 200.00 is chosen for alliance
Candidate qual = 1 salary = 120.00 is not chosen for alliance
Candidate qual = 2 salary = 80.00 is not chosen for alliance
Candidate qual = 2 salary = 120.00 is chosen for alliance
Candidate qual = 1 salary = 80.00 is not chosen for alliance
Visualize this by   https://tinyurl.com/3kduyfsc.

Now We know how to use Python. easily Now we will see the python basics.

Python has a format. It has very important property indentation to indicate a block of code. Indentation  is blank spaces in the beginning of the code.

def functionname():
statement 1

stament N

if you make mistake in indentation, you will get IndentationError: unexpected indent

Python has also constants, variables and some data structures like list, tuple, set, dictionary and also supports comprehension with lambda function. Here are some examples:

Python constants
print(5)            #   5      : int Constant

print (5.5) # 5.5 : float constant

print ('55') # '55' : string constant

Result:


5

5.5

55

Python variables

Variables do not need to be declared with any particular type, automatically they have been assigned according to the value you typed.

You can change type after they have been set.


i = 5 # integer variable i
print(i,type(i))


f = 5.5 # float variable f
print (f,type(f))

s = 'hello' # string variable s
print (s,type(s))

print(int(f),type(int(f))) # casting float to integer

print(str(i),type(str(i))) # casting integer to string


Result:

5 <class 'int'>
5.5 <class 'float'>
hello <class 'str'>
5 <class 'int'>
5 <class 'str'>

String variable can be declared using single quote or double quotes.

Variable Names

A variable can have a short name (like i, f  and s) or a more descriptive name (age, fname, first_name, lname, last_name,  city, ). 

Rules for Python variable names:

  • must start with a letter or the underscore character
  • cannot start with a number
  • can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
  • case-sensitive (age, Age and AGE are three different variables)

Examples:

tom = "Mickey", t_o_m = "Mickey",  _tom = "Mickey", tOm = "Mickey",TOM = "Mickey", tom1= "Mickey"

Many variables can be assigned values  in a single statement


i, f, s = 5, 5.5, 'Hello'

print(type(i), type(f),type(s))

Result:


<class 'int'> <class 'float'> <class 'str'>

Many values take single value.


x
, y, z = 'Apple', 'Bananas', 'Orange'
print(x, y, z)
print()

x
, y, z = "Orange", "Bananas", "Cherry"
print(x)
print(y)
print(z)

Result:

Apple Bananas Orange

Orange
Banana
Cherry

In python, we can assign the same value to many variables like this

x =  y = z = "Tom&Jerry"
print(x)
print(y)
print(z)

Result:


Tom&Jerry
Tom&Jerry
Tom&Jerry

Python supports same variable name for collection of values like list, tuple, set,  dictionary.

 NEXT


No comments:

Post a Comment

Making Prompts for Profile Web Site

  Prompt: Can you create prompt to craft better draft in a given topic. Response: Sure! Could you please specify the topic for which you...