Understanding python (Part -2)

Ignoramus
4 min readAug 16, 2020

Conditions:

Conditions in Python: they are same as conditions in literature or languages. They have various types as comparison, branching and logical operators. It covers all the if-else statements, AND, OR, NOT.

Comparison operators:

equal: ==,

not equal: !=,

greater than: >,

greater than or equal to: >=,

smaller than: <,

smaller than or equal to: <=,

They are useds in expressions and return boolean values. As example we can see:

Using comparison operators.

2>1

2>5

5>=2

5<=2

10 == 20

We can also use strings to use this operations.

“ACDC” == “One Direction”

Also as python is case sensitive and has difference in storing alphabets, we can check using the operators.

“About” > “Brief”

“About” <”Brief”

“A” == “a”

Conditional statements: Or branching, it allows to run different statement for different inputs. If conditions satisfies then a piece of code will run.

# Let us consider a example of temperature being fixed and called.

temperature = 35

if temperature > 30:

print(“It’s warm!”)

print(“Drink water.”)

print(“Done!”)

# We will add an else statement to the above condition:

temperature = 20

if temperature > 30:

print(“It’s warm!”)

print(“Drink water.”)

else:

print(“Done!”)

Here we can note that when the condition was true the set of code under if was executed and when the condition was false the set of code under if was skipped and jumped to else condition. We also can add mutliple conditions using el-if(short for else if).

temperature = 25

if temperature > 30:

print(“It’s warm!”)

print(“Drink water.”)

elif temperature > 20:

print(“It’s Fine!”)

print(“Drink water.”)

else:

print(“It’s cold!”)

print(“Done!”)

# trying with different temperature

temperature = 15

if temperature > 30:

print(“It’s warm!”)

print(“Drink water.”)

elif temperature > 20:

print(“It’s Fine!”)

print(“Drink water.”)

else:

print(“It’s cold!”)

print(“Done!”)

We can see that we have two different output for two different temperature.

We also have logical operators as AND, OR and NOT. This works the same way as it has it’s truth tables.

AND table:

OR table:

NOT table:

# we will see examples of the logical operators below:

# assume you are checking for eligibility to give away loan to those who have high income and good credits.

high_income = False

good_credit = True

student = True

if high_income and good_credit:

print(“Eligible!”)

else:

print(“Not eligible!”)

# Assume checking for the same but either of one can be considered.

high_income = False

good_credit = True

student = True

if high_income or good_credit:

print(“Eligible!”)

else:

print(“Not eligible!”)

# let the eligibility includes should not be a student.

high_income = False

good_credit = True

student = True

if (high_income or good_credit) and not student:

print(“Eligible!”)

else:

print(“Not eligible!”)

Here in the above codes we can see that when and operator was used it had to be both true to be eligible so it returned message for the else statement. In or operator one of them is true so it returned the message eligible. And in not where student is true and condition requires it not to be a student so not student is false so it returned else message back. This way we can see how the logical operator works.

Dictionaries :

This are collection of keys and their values. It holds a sequence of elements. Each element is represented by key and it’s corresponding value where the values can be integer, string, tuple. We can retrieve the value based on the name or using key. We can add, delete entries in it.

format: dict = {“Key1”:”Value1",”Key2":”Value2",…}

Dict = {“Key1”:1, “Key2”:”2", “Key3”:[3,3,3],”Key4":(4,4)}

Dict

Let say we are trying to keep data about a person:

Data = {‘id’:1,’name’:’Brook’,’age’:23}

Data

# adding

Data = {‘id’:1,’name’:’Brook’,’age’:23}

Data

Data[‘DOB’]=’24/3/1997'

Data

output : {‘id’: 1, ‘name’: ‘Brook’, ‘age’: 23, ‘DOB’: ‘24/3/1997’}

# deleting

Data = {‘id’:1,’name’:’Brook’,’age’:23}

Data

del(Data[‘age’])

Data

{‘id’: 1, ‘name’: ‘Brook’}

# accessing using keys​

Data = {‘id’:1,’name’:’Brook’,’age’:23}

print(Data[‘name’])

Brook

Set:

It is a unique collection of objects in python. Can be denoted by curly bracket ‘{ }’.

set1 = {“Pop”,”Rock”,”Soul”,”Hard rock”,”Rock”,”R&B”,”Pop”,”Rock”,”Disco”,”Country”,”Pop”}

set1

{‘Country’, ‘Disco’, ‘Hard rock’, ‘Pop’, ‘R&B’, ‘Rock’, ‘Soul’}

As we can see the multiple occurring objects or elements are considered as one and we get to see a set with unique elements and no repetition. The duplicates are automatically removed. We can also use operations as add, remove, can verify for existence of an object, can set logic operation.

A = {“Pop”,”Rock”,”Soul”,”Hard rock”,”Rock”,”R&B”,”Pop”,”Rock”,”Disco”,”Country”,”Pop”}

A

{‘Country’, ‘Disco’, ‘Hard rock’, ‘Pop’, ‘R&B’, ‘Rock’, ‘Soul’}

Adding to the set

A = {“Pop”,”Rock”,”Soul”,”Hard rock”,”Rock”,”R&B”,”Pop”,”Rock”,”Disco”,”Country”,”Pop”}

A.add(“Hip hop”)

A

{‘Country’, ‘Disco’, ‘Hard rock’, ‘Hip hop’, ‘Pop’, ‘R&B’, ‘Rock’, ‘Soul’}

Removing from the set

A = {“Pop”,”Rock”,”Soul”,”Hard rock”,”Rock”,”R&B”,”Pop”,”Rock”,”Disco”,”Country”,”Pop”}

A.remove(“Soul”)

A

{‘Country’, ‘Disco’, ‘Hard rock’, ‘Pop’, ‘R&B’, ‘Rock’}

Verifying of object in the set

A = {“Pop”,”Rock”,”Soul”,”Hard rock”,”Rock”,”R&B”,”Pop”,”Rock”,”Disco”,”Country”,”Pop”}

“Hard rock” in A

True

Similarly we can use difference(), union(), intersection() when we define two set.

--

--

Ignoramus

“If you torture the data long enough, it will confess to anything.”