Click here to view and download handwritten notes
Click here to view and download notes given by Professor
1.Comments¶
In [1]:
#this is a example of single line comment
In [4]:
"""this is a example of multiline comments""" #in multiline comments ("""___""") or('''___''') are used
Out[4]:
'this is a example of multiline comments'
2.Docstring¶
In [16]:
def add(a, b):
"""
This function adds two numbers and returns the result. #"""___""" or '''__''' are used for
multiline docstring and single line docstring respectivly.
:param a: The first number
:param b: The second number
:return: The sum of the two numbers
"""
return a + b
print(add.__doc__) #.__doc__ are used to display docstring
This function adds two numbers and returns the result. :param a: The first number :param b: The second number :return: The sum of the two numbers
3.Data type-Numeric¶
In [19]:
#Integer data type represents whole numbers, both positive and negative, without any decimal point. For example:
x=232
# Floating-point data type represents real numbers with a decimal point. It can also represent numbers in scientific notation. For example:
y=33.4
#Complex data type represents complex numbers in the form a + bj, where a and b are real numbers and j is the imaginary unit. For example:
z=23+23j
print(type(x))
print(type(y))
print(type(z))
<class 'int'> <class 'float'> <class 'complex'>
4.Compound Data type¶
List[].¶
In [21]:
my_list = [1, 2, 3, "hello", 5.67]
print("this a list",my_list)
# Ordered collection of elements.
# items of different data types.
# Mutable (elements can be changed after creation).
# Defined using square brackets [].
this a list [1, 2, 3, 'hello', 5.67]
Tuple()¶
In [22]:
my_tuple = (1, 2, 3, "world", 4.56)
print("this is an example of tuple",my_tuple)
# Ordered collection of elements.
# Allows items of different data types.
# Immutable (elements cannot be changed after creation).
# Defined using parentheses ().
this is an example of tuple (1, 2, 3, 'world', 4.56)
Sets{} or () for constructor¶
In [23]:
my_set = {1, 2, 3, 3, 4} # Note that duplicate elements are automatically removed.
print("this an example of set",my_set)
# Unordered collection of unique elements.
# Elements are of the same data type.
# Mutable (you can add and remove elements).
# Defined using curly braces {} or the set() constructor.
this an example of set {1, 2, 3, 4}
Dictionary{}¶
In [25]:
my_dict = {"name": "John", "age": 30, "city": "New York"}
print("this is an example of dictionary",my_dict)
# Collection of key-value pairs.
# Keys are unique and immutable.
# Keys are used to access values.
# Defined using curly braces {} with key-value pairs separated by colons :.
this is an example of dictionary {'name': 'John', 'age': 30, 'city': 'New York'}
Arrays[]¶
In [26]:
my_arry=[122,332,432]# same data type i.e integers
print("this is an example of array",my_arry)
# Ordered collection of elements of the same data type.
# Memory-efficient for large collections of data.
# Requires importing the array module.
this is an example of array [122, 332, 432]
5.Boolen Data type¶
In [29]:
# The Boolean data type in Python represents one of two values: True or False.
# Booleans are used to express logical conditions and are fundamental in control flow, conditional statements, and decision-making in Python programs.
x=True
y=False
print(type(x)) #its shows the bool class
print(type(y))
sa=12
hi=32
print(sa<hi) #returns True
print(sa>hi) #returns False
<class 'bool'> <class 'bool'> True False
6.Dictionary¶
In [31]:
#python program to demonstrate Dictionary
dict={"name":"sahil",
"rollno:":38,
"class":"FYBSC DS"
}
print("this is an example of dictionary",dict)
print(type(dict)) #to check data type
# Collection of key-value pairs.
# Keys are unique and immutable.
# Keys are used to access values.
# Defined using curly braces {} with key-value pairs separated by colons :.
this is an example of dictionary {'name': 'sahil', 'rollno:': 38, 'class': 'FYBSC DS'} <class 'dict'>
7.Sets¶
In [32]:
#python program to demonstrate Sets
#creating a sets
set={"sahil",3,"sahani",131}
print("this is an example of sets",set)
print(type(set))#to check data type
# Unordered collection of unique elements.
# Elements are of the same data type.
# Mutable (you can add and remove elements).
# Defined using curly braces {} or the set() constructor.
this is an example of sets {3, 131, 'sahani', 'sahil'} <class 'set'>
8.Mapping¶
In [2]:
def square(x):
return x ** 2
numbers = [1, 2, 3, 4, 5]
x = map(square, numbers) # map(function(square),iterable(numbers))
#use of map function
#map function is a built-in function that is used to apply a specified function to each item in an iterable.
#The map() function is often used when you want to perform the same operation on every item in a collection and collect the results.
# sytax = map(function, iterable, ...)
# To get the results as a list, you can convert the map object to a list.
y = list(x)
print("this is an output after applyed map functon",y)
this is an output after applyed map functon [1, 4, 9, 16, 25]
9.Basic Elements of Python¶
Objects¶
In [3]:
# 1)integers: 1,23,-33
# 2)float: 12.3,22.3
# 3)boolen: True,false
# 4)None:
In [4]:
2+4
Out[4]:
6
In [5]:
2.0+4.0
Out[5]:
6.0
In [16]:
type(2+1.3)
Out[16]:
float
In [17]:
3==3 #use double equal operators to to compare
Out[17]:
True
In [12]:
3!=3 #used not equal operators to compare
Out[12]:
False
In [13]:
3/2
Out[13]:
1.5
In [15]:
type(3/2)#shows the float type
Out[15]:
float
In [18]:
2//2
Out[18]:
1
In [19]:
type(2//2)
Out[19]:
int
In [44]:
3//2 #use // to get Quotient
Out[44]:
1
In [21]:
type(3//2)
Out[21]:
int
In [24]:
5%2 #use % to get Remainder
Out[24]:
1
In [25]:
7/3
Out[25]:
2.3333333333333335
In [26]:
7//3
Out[26]:
2
In [27]:
7%2
Out[27]:
1
In [34]:
3**2 #used **2 to get square of that value
Out[34]:
9
In [30]:
type(3**2)
Out[30]:
int
In [31]:
2.3**2
Out[31]:
5.289999999999999
In [33]:
type(2.3**2)
Out[33]:
float
In [35]:
3**3
Out[35]:
27
comparison operators¶
In [36]:
4==4 #equal to
Out[36]:
True
In [37]:
4==5
Out[37]:
False
In [38]:
4!=4 #not eqaual to
Out[38]:
False
In [39]:
4!=5
Out[39]:
True
In [40]:
5>=6 #grrater than or equal to
Out[40]:
False
In [41]:
5>=5
Out[41]:
True
In [42]:
5>4 #greater than
Out[42]:
True
In [43]:
4<5 #less than
Out[43]:
True
In [45]:
3+2*6
Out[45]:
15
In [46]:
(3+2)*6
Out[46]:
30
AND , OR operators¶
In [50]:
x=10
print(x<20 and x>5)#return true
#AND operator are use compare this value
#in this both the condition should be true
True
In [49]:
x=10
print(x<20 or x>5)#return true
#OR operator are used to compare this values
#in this atleast one condition should be true
True
In [56]:
print(10<20 and 10>89)
#returns false 10 is smaller than 20
# but 10 is not greater than 89
#both condition should be true for True
False
In [57]:
print(10<5 or 10>20)
#returns false
#10 is not smaller thn 5 and 10 is not greater than 20
#one condition should be true for True
False
10.Input function¶
In [3]:
print(input("what is your name"))
print(int(input("how old are your")))
print(float(input("what is your height")))
sahil sahani
19
5.5
In [4]:
x=input("Enter your name:")
print("My name is",x)
y=int(input("Enter your age:"))
print("My age is",y)
z=float(input("Enter your height:"))
print("My height is",z)
My name is sahil sahani
My age is 19
My height is 5.5
In [7]:
a=int(input("Enter first number:"))
b=int(input("Enter second number:"))
print("this is an addition of a and b:",a+b)
this is an addition of a and b: 70
11.Output statement¶
In [8]:
#print function are used for output statement
print("hello") #we can use "__" for output
print('hellow') #we can use '__' for output
print("sahil","sahani") # returns sahil sahani because of (,)
print("sahil","sahani","data",sep="**")
# returns sahil**sahani**data because of sep="__"
#which by default gives space after (,) and not visible
print(13) #it can also print numerics data type
hello hellow sahil sahani sahil**sahani**data 13
In [10]:
print("sahil")
print("sahani")
#By default there is end="\n" are available which is not visiblle
print("sahil",end=" ") #returns sahil sahani because of end="_" and by removing (\n)
print("sahani")
print("sahani",end="\t") #there will be creat a space according charachters if smaller the char.. larger the space and vice versa.
print("sahil",end="\t")
print("sanjay")
sahil sahani sahil sahani sahani sahil sanjay
In [11]:
x=10
y=20
print(x,y)
print(x,y,sep="##")
10 20 10##20
In [14]:
name="sahil"
age=19
print("My name is",name,"and My Age is",age,)
My name is sahil and My Age is 19
12.loop Statement¶
For loop¶
In [7]:
#for loop
for i in range(1,10,2):
print(i)
1 3 5 7 9
While loop¶
In [17]:
#while loop
x=0
while x<5:
x=x+1
print("sahil")
sahil sahil sahil sahil sahil
13.The else statement¶
In [14]:
x=0
while x<5: # Code to execute when the condition becomes False
x=x+1
print("sahil")
else:
print("yes") # Code to execute when the condition becomes False
sahil sahil sahil sahil sahil yes
14.Break statement¶
In [2]:
x=0
while x<10:
print(x)
if x==5:
break #break statement are used to break statement
x=x+1
0 1 2 3 4 5
15.Continue statement¶
In [4]:
x=0
while x<10:
x=x+1
if x==5:
continue #continue statement are used to break at 5 and then continue after that..
print(x)
1 2 3 4 6 7 8 9 10
16.Pass statement¶
In [9]:
#python program to demonstrate
#Pass statement
if 5>20:
pass
else:
print("when condition in false")
when condition in false
In [11]:
#similarly when when condition is true nothing will happen
if 15<20:
pass
else:
print("when condition is false")
17.Assert statement¶
syntax:- assert condition,error_message¶
In [16]:
#when assert statement is True
def divide(a, b):
assert b != 0, "Division by zero is not allowed"
return a / b
result = divide(10, 2) # This is fine, no assertion error
print(result)
5.0
In [15]:
#when assert statement is False
def divide(a, b):
assert b != 0, "Division by zero is not allowed"
return a / b
result = divide(10, 0) # This will raise an AssertionError with the specified message
print(result)
--------------------------------------------------------------------------- AssertionError Traceback (most recent call last) Cell In[15], line 5 3 assert b != 0, "Division by zero is not allowed" 4 return a / b ----> 5 result = divide(10, 0) # This will raise an AssertionError with the specified message 6 print(result) Cell In[15], line 3, in divide(a, b) 2 def divide(a, b): ----> 3 assert b != 0, "Division by zero is not allowed" 4 return a / b AssertionError: Division by zero is not allowed
18.Return statement¶
In [24]:
def sahil(a,b):
x=a*b
y=a+b
return x,y
int1=10
int2=5
sahil(int1,int2)
Out[24]:
(50, 15)
In [25]:
def add(a, b):
result = a + b
return result # Returns the sum of a and b
sum_result = add(5, 3)
print("The sum is:", sum_result)
def greet(name):
greeting = "Hello, " + name
return greeting # Returns a greeting message
greeting_message = greet("Alice")
print(greeting_message)
The sum is: 8 Hello, Alice
In [ ]: