Tuesday, 29 March 2022

Fundas2

Some Basic Functions


# for <loop_variable> in <iterable>:
# <code>
for i in range(10):
print(i)
print(25*'-')
# for <loop_variable> in range(<start>, <stop>, <step>):
# <code>
for i in range(1,10,2):
print(i)
for i in range(1,30,2):
print('fun'*i)
for i in range(30,1,-2):
print('fun'*i)
l = ['apple','boy','cat','dog']
# for loop with iterable list
for i in range(len(l)):
print(l[i])
str = ' I am iterable by seperation?'
for i in str:
print(i)


# Break
lis = [1, 2, 3, 4, 5]


for elem in lis:
if elem % 2 == 0:
print("Even:", elem)
print("break")
break
else:
print("Odd:", elem)
"""
Odd: 1
Even: 2
break
"""
# Continue

for elem in lis:
if elem % 2 == 0:
print("continue")
continue
print("Odd:", elem)
"""
Odd: 1
continue
Odd: 3
continue
Odd: 5
"""

# zip() is an amazing built-in function that we can use in Python
# to iterate over multiple sequences at once,
# getting their corresponding elements in each iteration

list1 = [10, 20, 30, 40]
list2 = [50, 60, 70, 80]
list3 = ['a','b','c','d']

for elem1, elem2,elem3 in zip(list1, list2, list3):
print(elem1, elem2, elem3)
"""
10 50 a
20 60 b
30 70 c
40 80 d
"""


# while <condition>:
# <code>

x=5
while x >= 0:
print("Fun " * x)
x -= 1

#nested for loop
dim =3
for i in range(dim):
for j in range(dim):
for k in range(dim):
print(i,j,k)
print('\n')
print('\n') # 3 x 3 3 = 9 elements x 9

num_cols=5
num_rows=5
for i in range(5):
for num_cols in range(num_rows-i):
print("*", end="")
print()


# Function in pytho

def fun1():
print('def()')

fun1()

def add(x,y):
print(x+y)
add(5,5)


def mulp(a, b=5): # default value for b
print(a * b)

mulp(10) #50


# Recursibve Factorial function

def fact(n):
if n == 0 or n == 1:
return 1
else:
return n * fact(n-1)

f = fact(5)
print(f)#120

def fib(n):
if n == 0 or n == 1:
return n
else:
return fib(n-1) + fib(n-2)
print(5*'-')
f = fib(4)
print(f) #3

Class : Object oriented concepts in python using class

class student:
name =
'Steve Jobs',
co = 'Apple',
country = 'USA',
sal = 1000000

def __init__(self,param1, param2, param3, param4):
self.name = param1
self.co = param2
self.country = param3
self.sal = param4

def display(self, param1,param2, param3, param4):
print(param1,param2,param3, param4)


stu = student(
'Bill Gates','Micro Soft','USA',100000)
print(stu.name, stu.sal) # Bill Gates 100000

print(type(stu)) #<class '__main__.student'>
print(stu) # <__main__.student object at 0x000001ACB0C19FD0>
print(stu.co,stu.country) #Micro Soft USA

# del stu

#---------------------------------------------
class Student:

def __init__(self, name):
self._name = name

@property
def name(self):
print("Calling getter")
return self._name

@name.setter
def name(self, new_name):
print("Calling setter")
self._name = new_name

@name.deleter
def name(self):
print("Calling deleter")
del self._name

stu = Student(
"Sandilya")
print(stu.name)
stu.name =
"Chandra"
print(stu.name)

del stu
"""
Calling getter
Sandilya
Calling setter
Calling getter
Chandra Gupta
"""


All about import statement

Many ways we can import a python Libraries we will see the various ways
import pandas
print(pandas.read_csv('bio.csv'))

import pandas as pd # renamed as pd
df = pd.read_csv('bio.csv')
print(df)

from pandas import read_csv # import only read_csv
print(read_csv('bio.csv'))

"""
Unnamed: 0 Name Age
0 0 Raj 23
1 1 Ram 23
2 2 Sita 24
3 3 Laks 21
"""

from pandas import * # import all
print('df:',read_csv('bio.csv'))
"""
df: Unnamed: 0 Name Age
0 0 Raj 23
1 1 Ram 23
2 2 Sita 24
3 3 Laks 21
"""

List Comprehension in Python


The syntax used to define list comprehensions usually follows one of these four patterns:

  • [<value_to_include> for <var> in <sequence>]
  • [<value_to_include> for <var1> in <sequence1> for <var2> in <sequence2>][<value_to_include> for <var> in <sequence> if <condition>]
  • [<value> for <var1> in <sequence1> for <var2> in <sequence2> if <condition>]

# [<value_to_include> for <var> in <sequence>]
#print all alphabets
print([chr(i) for i in range(65, 91)])
#['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']

# [<value_to_include> for <var1> in <sequence1> for <var2> in <sequence2>][<value_to_include> for <var> in <sequence> if <condition>]
print([k for k in range(1, 25) if k % 2 == 0])
#[2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24]
# [<value> for <var1> in <sequence1> for <var2> in <sequence2> if <condition>]
print([i * j for i in range(1, 5) for j in range(1, 5) if i % j == 0])
# [1, 2, 4, 3, 9, 4, 8, 16]
Above sample code snippets will explain how to use list comprehension with for, nested for, and if condition.

# Brain Teaser See the differenc  between the below statements

import sys
print(sys.getsizeof([i for i in range(500)])) # 4216

print(sys.getsizeof((i for i in range(500)))) #112

Here all elements in a list and versus one element in a list at a time. That's why, there is difference in memory allocation.








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...