Wednesday, May 10, 2023

#PYTHON(DAY14)if-elif-else, Nested if

#PYTHON(DAY14)

Continuation of conditional statements

if-elif-else

  • primary block will be ‘if’, this will evaluate the c.e and if it is true,
  • it will execute code-block under ‘if’ statement. if the c.e turns out to false, then it will go to first ‘elif’ block
  • ‘elif’ c.e is validated and if it is True, the code-block under this ‘elif’ will be executed, if not, it will go to next elif block and so on till it validates all the elif blocks.
  • if none of the ‘if’, ‘else’ c.e if turns to be True, then else block will be executed

flow chart of if-elif-else:

if-elif-else

Example

z = int(input('enter marks:')) 


if z>80:
print('first class with distinction')

elif z>60 and z<80:
print('first class')

elif z>35 and z<60:
print('second class')

elif z<35:
print('fail')

And the output is:

enter marks:46
second class

Nested if

  • if inside the if

flow chart of Nested if:

nested if

Example

marks = int(input('enter marks:'))

c = time.time()
a = time.time()
if marks>=35:
if marks<60:
print('second class')
elif marks>60 and marks<80:
print('first class')
else:
print('fwd')
else:
print('fail')

d = time.time()
b = time.time()

And the output is:

enter marks:67
first class

Tuesday, May 9, 2023

#PYTHON(DAY13)Conditional statements

 

#PYTHON(DAY13)

Conditional statements

Python’s conditional statements carry out various calculations or operations according to whether a particular Boolean condition is evaluated as true or false.

Indentation:

If statement, without indentation (will raise an error):

a = 33
b = 200
if b > a:
print("b is greater than a")
And the output is:

File "demo_if_error.py", line 4
print("b is greater than a")
^
IndentationError: expected an indented block

if statement

  • syntax of ‘if’
  • ‘if’ conditional_expression :
  • code_block

flowchart:

IF STATEMENT

syntax:

if <conditional expression>  
Statement

Example:

a, b = 6, 5  

# Initializing the if condition
if a > b:
code = "a is greater than b"
print(code)

And the output is:

a is greater than b

if-else:

  • else the c.e is false, then the code_block under ‘else’ will be executed
  • ‘else’ does not have a c.e

flow chart of if-else

if-else

syntax:

if <conditional expression>  
Statement
else
Statement

Example:

a = 200
b = 33
if b > a:
print("b is greater than a")
else:
print("b is not greater than a")

And the output is:

a is greater than b

Monday, May 8, 2023

#PYTHON(DAY12)dictionaries

 

#PYTHON(DAY12)

dictionaries

  • dictionaries are ‘key’ & ‘value’ pairs enclosed in ‘{}’
  • ‘key’ and ‘value’ are seperated by ‘:’ while ‘key and value’ pairs are seperated by ‘;’
  • ‘keys’ are mapped to their ‘values’
  • in dictionaries, ‘keys’ are unique, we can not have duplicate keys, we can have duplicate values
  • dictionaries are unordered & mutable. ‘keys’ of dictionaries can be used to iterate over values
  • ‘{}’ is an empty dictionary
  • dict() is class of dictionary
x = {} # empty dictionary
print(x, type(x))

x = {'key1':1,
'key2':2,
'l1':'eleven',
3:'three',
'key1':100,
'key3':200
}
print(x,type(x))

And the output is:

{} <class 'dict'>
{'key1': 100, 'key2': 2, 'l1': 'eleven', 3: 'three', 'key3': 200} <class 'dict'>
x['key1']
And the output is:
<function print>
x['key2']
And the output is:
[1, 2, 3]
x['key1'](type(['key2']))
And the output is:
<class 'list'>
print(dir(x))

And the output is:

['__class__', '__class_getitem__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__ior__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__or__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__ror__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'clear', 'copy', 'fromkeys', 'get', 'items', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values']
  • update method is used to add ‘key’ and ‘value’ pair
  • syntax — -> x.update({new_key:value})
x.update({'key6':100})
print(x)
And the output is:
{'key1': <built-in function print>, 'key2': [1, 2, 3], 'key3': (11, 12, 13), 'key4': {4, 5, 6}, 'key5': 'print', 'key6': 100}
# we can also add a key and value pair in following fashion
# x[new_key] = value

x['xyz'] = {'abc':123}
And the output is:

{'key1': <function print>,
'key2': [1, 2, 3],
'key3': (11, 12, 13),
'key4': {4, 5, 6},
'key5': 'print',
'key6': 100,
'xyz': {'abc': 123}}
x['xyz'] = {'def':456} # overriding the old key and value
x
And the output is:

{'key1': <function print>,
'key2': [1, 2, 3],
'key3': (11, 12, 13),
'key4': {4, 5, 6},
'key5': 'print',
'key6': 100,
'xyz': {'def': 456}}
  • we use get() to fetch a value
x.get('xyz')
And the output is:
{'def': 456}
# we can also fetch a value in following fashion
x['xyz']
{'def': 456}
  • keys(), values() and items() returns a dict_keys, dict_values, dict_items objects.
  • These objects have the keys, values and key & value and tuples of’key and value pairs as elements respectively
  • we need to type cast these objects in lists or tuple to see them
a,b,c = x.keys(), x.values(), x.items()

print(a,b,c, sep ='\n')
print()
print(type(a), type(b), type(c))
And the output is:

dict_keys(['key1', 'key2', 'key3', 'key4', 'key5', 'key6', 'xyz'])
dict_values([<built-in function print>, [1, 2, 3], (11, 12, 13), {4, 5, 6}, 'print', 100, {'def': 456}])
dict_items([('key1', <built-in function print>), ('key2', [1, 2, 3]), ('key3', (11, 12, 13)), ('key4', {4, 5, 6}), ('key5', 'print'), ('key6', 100), ('xyz', {'def': 456})])

<class 'dict_keys'> <class 'dict_values'> <class 'dict_items'>
  • pop takes exactly one input/arg i.e the key
  • we can capture this popped ‘value’ in a var for further use
bb = x.pop('key6')
print(bb)
100
x
And the output is:

{'key1': <function print>,
'key2': [1, 2, 3],
'key3': (11, 12, 13),
'key4': {4, 5, 6},
'key5': 'print'}
  • popitem() will pop the last key & value pair if no input is given
  • if an key is given as input, it will pop that key and value pair as tuple
bb = x.popitem()
print(bb)
x
('key5', 'print')
{'key1': <function print>,
'key2': [1, 2, 3],
'key3': (11, 12, 13),
'key4': {4, 5,6}}
  • it will create a new dict from the keys of given dict, values by default will be none
  • fromkeys() is a class method — -> we will see this in oops
list(x.keys())
['key1', 'key2', 'key3', 'key4']
y = dict.fromkeys(x)
print(y)
{'key1': None, 'key2': None, 'key3': None, 'key4': None}
  • The setdefault() method returns the value of the item with the specified key.
  • we use to set the default value, if it is not present in dictionary.
  • if the key does not exist, then it returns ‘None’
  • if we give the default key, value pairs then it display the default value, when the defaultkey is called
x = {1:'milk',
2:'tea powder',
3:'sugar',
4:[1,2,3],
5:{4,5,6}}
print(x)
y = x.setdefault("6")
print(y)

And the output is:

{1: 'milk', 2: 'tea powder', 3: 'sugar', 4: [1, 2, 3], 5: {4, 5, 6}}
None
# to pull a value without a key or when there is no key
x.get('hello')
x

Sunday, May 7, 2023

#PYTHON(DAY11)frozenset()

#PYTHON(DAY11)

frozenset( )

It is immutable and it is hashable. It is also called an immutable set. Since the elements are fixed, unlike sets you can’t add or remove elements from the set. Frozensets are hashable, you can use the elements as a dictionary key or as an element from another set.

frozenset(iterable)

s = {1,2,3,4,}
a = frozenset(s)
print(a, type(a))

And the output is:

frozenset({1, 2, 3, 4}) <class 'frozenset'>

Note: There is no method like add in frozenset

s = {1,2,3,4,}
a = frozenset(s)
a.add(5)
print(a)

for the above code we get an error saying that ‘frozenset’ object has no attribute ‘add’.

 

Building Static Website(part6) HTML Lists

  Building Static Website (part6) HTML Lists Today, let us add some lists to our detailed view section by using html lists. Lists: List is a...