1st Unit - Jupyter Notebook
1st Unit - Jupyter Notebook
Python Basics
In [10]:
Out[10]:
'8*9'
In [14]:
hello
In [15]:
if(x==5): # If condition
print("odd")
print("even")
even
In [30]:
In [31]:
<class 'float'>
In [32]:
VAL=3.14
In [33]:
print(VAL)
3.14
In [35]:
10
15
In [1]:
print(1, 2, 3, 4)
print(1, 2, 3, 4, sep='*') # Print attributes Sep & End
print(1, 2, 3, 4, sep='#', end='&')
1 2 3 4
1*2*3*4
1#2#3#4&
In [4]:
In [7]:
In [15]:
x=input("x val")
y=input("y val")
eval('int(x)+int(y)') #Type Casting
#print(int(x)+int(y))
x val4
y val5
Out[15]:
In [8]:
x=78
print(format(x,'b')) #Printing Binary Format
print(chr(x)) #Printing ASCII Format
c=x>>2
print(format(c,'b'))
1001110
10011
In [25]:
print(format(312,'b'))
100111000
In [7]:
a=2
b=4
a=7
print(id(a)) # Priting the Memory Address of the Variable
print(id(2))
print(id(b))
print(id(4))
print(id(7))
1886069942768
1886069942608
1886069942672
1886069942672
1886069942768
In [16]:
c:\time\newfolder\file
In [23]:
a=10
b=3.65
c='hello'
print(f'int = {a} float = {b} string = {c}')
In [ ]:
Arithmetic + - * / // % **
Logical && || ! and or not
Comparison < > <= >= != ==
Bitwise & | ^ ~ >> <<
Membership is, is not, in, not in
Assignment += -= /= *=
In [9]:
#Arithmetic Operators
print("Addition =",a + b)
print("Subtraction =",a - b)
print("Multiplication =",a * b)
print("Division =",a / b)
print("Floor Division =",a // b)
print("Modilus =",a % b)
#Comparison Operators
print("is a greater than b :",a > b)
print("is a less than b :",a < b)
print("is a equals to b :",a == b)
print("is a no equals to b :",a != b)
print("is a Greater than Equal to b :",a >= b)
print("is a Less than Equal to b :",a <= b)
#Logical Operators
x = True
y = False
print("True and False =",x and y)
print("True or False =",x or y)
print("not False =",not y)
#Bitwise Operators
print(f"Bitwise AND of {a}, {b} =",a & b)
print(f"Bitwise OR of {a}, {b} =",a | b)
print(f"Bitwise NOT of {a} =",~a)
print(f"Bitwise XOR of {a}, {b} =",a ^ b)
print(f"Bitwise Right shift of {a} =",a >> 2)
print(f"Bitwise Left shift of {a} =",a << 2)
#Identity Operator
print("if the operands are not identical ",a is not b)
print("if the operands are identical ",a is b)
list = [10, 20, 30, 40, 50]
if (a not in list):
print("x is NOT present in given list")
else:
print("x is present in given list")
if (b in list):
print("y is present in given list")
else:
print("y is NOT present in given list")
Enter vaue : 5
Enter vaue : 6
Addition = 11
Subtraction = -1
Multiplication = 30
Division = 0.8333333333333334
Floor Division = 0
Modilus = 5
is a equals to b : False
is a no equals to b : True
Bitwise AND of 5, 6 = 4
Bitwise OR of 5, 6 = 7
Bitwise NOT of 5 = -6
Bitwise XOR of 5, 6 = 3
In [28]:
50
In [10]:
a=int(input("enter a value"))
b=float(input("enter float :"))
print(type(a))
print(type(b))
print(a+b)
enter a value45
<class 'int'>
<class 'float'>
110.23
In [27]:
a=189
print(format((a),'b'))
print(format((a>>2),'b'))
print(a>>2)
10111101
101111
47
In [32]:
print('rama'+'seeta')
ramaseeta
In [34]:
print("rama",56)
rama 56
Sample Code
In [16]:
if guess != age:
print("Wrong!")
else:
print("Correct")
Guess: 4
Wrong!
In [6]:
correctNumber = 5
guess = 0
if guess != correctNumber:
print('False guess')
False guess
False guess
False guess
False guess
Out[9]:
In [47]:
0.9288252486859883
8576712
8740158.580295661
[8, 7, 4, 10, 2, 9, 6, 3, 5, 1]
[7]
In [15]:
45
20
39
37
39
In [16]:
<class 'list'>
In [20]:
In [23]:
print(ls[3:-1]) # Slicing
In [5]:
Out[5]:
56
In [6]:
Out[6]:
In [13]:
a= int(input("enter a value"))
print(type(a))
enter a value4
<class 'int'>
enter values47,54
Out[31]:
(47, 54)
In [9]:
a=list(map(int,input().split()))
4 5 4 6 8 7
In [10]:
Out[10]:
[4, 5, 4, 6, 8, 7]
In [22]:
a,b,c,d,e,f=1,3,2,4,5,6
print(eval('a*b+(c-d)/e**f'))
print(a*b+(c-d)/e**f)
2.999872
2.999872
In [25]:
print(a*b,c-d,e**f)
3 -2 15625
In [28]:
-2/15625
Out[28]:
-0.000128
Conditional Statments
In [4]:
#if-else statement
if n%2 == 0 :
#if block
print("Given number %d is even " %n)
else:
#else block
print("Given number %d is odd "%n)
Enter a value : 46
In [17]:
if m >= 90 :
#if block
print("The grade of a student is O")
elif m<90 and m>=80 :
#elif block
print("The grade of a student is A+")
elif m<80 and m>=70 :
#elif block
print("The grade of a student is A")
elif m<70 and m>=60 :
#elif block
print("The grade of a student is B")
elif m<60 and m>=50 :
#elif block
print("The grade of a student is C")
elif m<50 and m>=40 :
#elif block
print("The grade of a student is D")
elif m<40 :
#elif block
print("The grade of a student is F")
In [10]:
# Even Odd
a = int(input("enter a value"))
if a%2 ==0:
print("even")
else:
print("odd")
enter a value77
odd
In [13]:
# Nested IF-ELSE
x,y,z=map(str,input("enter the department marks and attendence").split())
if x=='it':
if int(y)>78:
if float(z)>65:
print("you can enter your appication")
else:
print("not enough attendence")
else:
print("not enough marks")
else:
print("only it students can enter")
In [11]:
sal=int(input("Enter n Value"))
if sal==10000 or sal==25000 :
print("It is If section %d " %(sal))
else:
print("you are in else block, n value="+str(sal))
Enter n Value21000
In [14]:
if m >= 90 :
#if block
print("The grade of a student is O")
elif m<90 and m>=80 :
#elif block
print("The grade of a student is A+")
elif m<80 and m>=70 :
#elif block
print("The grade of a student is A")
elif m<70 and m>=60 :
#elif block
print("The grade of a student is B")
elif m<60 and m>=50 :
#elif block
print("The grade of a student is C")
elif m<50 and m>=40 :
#elif block
print("The grade of a student is D")
elif m<40 :
#elif block
print("The grade of a student is F")
else:
print("not Attended")
In [13]:
if a>b:
if a>c:
print("%d is the greatest of given 3 numbers"%a)
else:
print("%d is the greatest of given 3 numbers"%c)
else:
if b>c:
print("%d is the greatest of given 3 numbers"%b)
else:
print("%d is the greatest of given 3 numbers"%c)
Loops
In [8]:
n=int(input("Enter number"))
for i in range(1,21):
print(n,"x",i,"=",n*i)
4 x 2 = 8
4 x 3 = 12
4 x 4 = 16
4 x 5 = 20
4 x 6 = 24
4 x 7 = 28
4 x 8 = 32
4 x 9 = 36
4 x 10 = 40
4 x 11 = 44
4 x 12 = 48
4 x 13 = 52
4 x 14 = 56
4 x 15 = 60
4 x 16 = 64
4 x 17 = 68
4 x 18 = 72
4 x 19 = 76
4 x 20 = 80
In [7]:
# Amstrong Number
n=int(input("Enter number"))
l=len(str(n))
sum=0
t=n
while(n>0):
r=n%10
sum=sum+r**l
n=n//10
if(sum==t):
print("Amstrong Number")
else:
print("Not an Amstrong")
Enter number145
Not an Amstrong
In [28]:
Out[28]:
12.5625
In [31]:
price + _ #We reference '_' but this expression is now assigned to '_'
print(round(_, 2))
print(price)
12.56
100.5
In [3]:
x = 101
#Global variable in function
def udefinedFunction():
global x
print(x)
i=25
print(i)
x = 'Welcome To Vardhaman'
print(x)
udefinedFunction()
print(i+5) # i is not defined in local code segment
101
25
Welcome To Vardhaman
---------------------------------------------------------------------------
~\AppData\Local\Temp/ipykernel_14620/3133391172.py in <module>
9 print(x)
10 udefinedFunction()
---> 11 print(i+5)
In [ ]: