[go: up one dir, main page]

0% found this document useful (0 votes)
58 views16 pages

1st Unit - Jupyter Notebook

This document contains code snippets demonstrating various Python basics including: 1) Variable declaration and assignment, print statements, comments, arithmetic and comparison operators, conditional statements like if/else. 2) Functions like range(), random(), map(), split() for taking multiple inputs. 3) Data structures - lists, slicing, appending. Evaluation using eval() and compilation using compile(). The document covers Python fundamentals and provides examples of basic programming concepts.

Uploaded by

anyone
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views16 pages

1st Unit - Jupyter Notebook

This document contains code snippets demonstrating various Python basics including: 1) Variable declaration and assignment, print statements, comments, arithmetic and comparison operators, conditional statements like if/else. 2) Functions like range(), random(), map(), split() for taking multiple inputs. 3) Data structures - lists, slicing, appending. Evaluation using eval() and compilation using compile(). The document covers Python fundamentals and provides examples of basic programming concepts.

Uploaded by

anyone
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

1st Unit

Python Basics
In [10]:

"8*9" #Sample Caliculation

Out[10]:

'8*9'

In [14]:

x='hello';print(x) # Multy line code as Single line code

hello

In [15]:

if(x==5): # If condition
print("odd")
print("even")

even

In [30]:

abc # Variable Declaration


xyz='hi'
klm=4.67
bmn=0.0

In [31]:

print(type(bmn)) # Variable type

<class 'float'>

In [32]:

VAL=3.14

In [33]:

print(VAL)

3.14

In [35]:

a,b,c=5,10,15 # Multiple variable Assignement


print (a)
print (b)
print (c)

10

15

Print Formats and Attributes

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]:

x = 5; y = 10 # Print Statment Formats


print('The value of x is {} and y is {}'.format(x,y))
print('I love {0} and {1}'.format('vardhaman','college'))
print('I love {1} and {0}'.format('vardhaman','college'))
print('Hello {name}, {greeting}'.format(greeting = 'Goodmorning', name = 'John'))

The value of x is 5 and y is 10

I love vardhaman and college

I love college and vardhaman

Hello John, Goodmorning

In [7]:

x = 12.3456789 # Print Statment Formats


print('The value of x is %.4f' %x)
print('The value of x = %.2f and y is %d' %(x,y))

The value of x is 12.3457

The value of x = 12.35 and y is 10

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]:

h='iam from vardhaman'


v='hyderabad'
print(r"c:\time\newfolder\file")

c:\time\newfolder\file

In [23]:

a=10
b=3.65
c='hello'
print(f'int = {a} float = {b} string = {c}')

int = 10 float = 3.65 string = hello

All the Operators

In [ ]:

Arithmetic + - * / // % **
Logical && || ! and or not
Comparison < > <= >= != ==
Bitwise & | ^ ~ >> <<
Membership is, is not, in, not in
Assignment += -= /= *=
In [9]:

a = int(input("Enter vaue : "))


b = int(input("Enter vaue : "))

#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 greater than b : False

is a less than b : True

is a equals to b : False

is a no equals to b : True

is a Greater than Equal to b : False

is a Less than Equal to b : True

True and False = False

True or False = True

not False = True

Bitwise AND of 5, 6 = 4

Bitwise OR of 5, 6 = 7

Bitwise NOT of 5 = -6

Bitwise XOR of 5, 6 = 3

Bitwise Right shift of 5 = 1

Bitwise Left shift of 5 = 20

if the operands are not identical True

if the operands are identical False

x is NOT present in given list

y is NOT present in given list

In [28]:

a=input("enter the value: ")


print("the value yoy enterd is =",a)
print(int(a)+5)

enter the value: 45

the value yoy enterd is = 45

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

enter float :65.23

<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]:

# Guess my age game


age = 24

print ("Guess my age, you have 1 chances!")


guess = int(input("Guess: "))

if guess != age:
print("Wrong!")
else:
print("Correct")

Guess my age, you have 1 chances!

Guess: 4

Wrong!

In [6]:

correctNumber = 5
guess = 0

while guess != correctNumber:


guess = int(input("Guess the number: "))

if guess != correctNumber:
print('False guess')

print('You guessed the correct number')

Guess the number: 4

False guess

Guess the number: 2

False guess

Guess the number: 9

False guess

Guess the number: 0

False guess

Guess the number: 5

You guessed the correct number

Range and Random Function


In [9]:

# Range(lower_bound, upper_bound, step_size)


list(range(2,15,3))

Out[9]:

[2, 5, 8, 11, 14]

In [47]:

from random import *


print(random())
print(randint(1, 1_00_00_000)) #Genarate Random int Number
print(uniform(1, 1_00_00_000)) #Genarate Random float Number

items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]


shuffle(items) # Shuffles the list items
print(items)

items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]


x = sample(items, 1) # Pick a random item from the list
print(x)

0.9288252486859883

8576712

8740158.580295661

[8, 7, 4, 10, 2, 9, 6, 3, 5, 1]

[7]

In [15]:

x=39 # Global Variable


def sam():
i=20 # Local Variable
print(i)
print(x)
def sam2():
j=37
print(j)
print(x)
k=45
print(k)
sam()
sam2()

45

20

39

37

39

In [16]:

ls=[1,2,3,'hi',3.67,5,8.9] #List Declaration


print(ls)
print(type(ls))

[1, 2, 3, 'hi', 3.67, 5, 8.9]

<class 'list'>

In [20]:

ls.append(4) # Addind into list


print(ls)

[1, 2, 3, 'hi', 3.67, 5, 8.9, 4, 4, 4]

In [23]:

print(ls[3:-1]) # Slicing

['hi', 3.67, 5, 8.9, 4, 4]

In [5]:

eval("sum([8, 16, 32])") # Evaluation

Out[5]:

56

In [6]:

code = compile("5 + 4", "<string>", "eval")


eval(code)

Out[6]:

In [13]:

a= int(input("enter a value"))
print(type(a))

enter a value4

<class 'int'>

Multiple Values as Inputs


In [31]:

a,b =map(int,input("enter 2 values").split(',')) #Taking multiple values at the same time


a,b

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]:

n=int(input("Enter a value : "))

#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

Given number 46 is even

In [17]:

m=int(input("Enter the marks of DVTP Course"))

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")

Enter the marks of DVTP Course87

The grade of a student is A+

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")

enter the department marks and attendenceit 78 60

not enough marks

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

you are in else block, n value=21000

In [14]:

# AIM: Checking the grade of a student in particular course

m=int(input("Enter the marks of DVTP Course"))

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")

Enter the marks of DVTP Course49

The grade of a student is D

In [13]:

a,b,c = map(int,input("Enter 1st vaue : ").split())

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)

Enter 1st vaue : 4 5 6

6 is the greatest of given 3 numbers

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]:

tax = 12.5 / 100


price = 100.50
price * tax #this is assigned to '_' and we use it in the next expression

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

---------------------------------------------------------------------------

NameError Traceback (most recent call last)

~\AppData\Local\Temp/ipykernel_14620/3133391172.py in <module>

9 print(x)

10 udefinedFunction()

---> 11 print(i+5)

NameError: name 'i' is not defined

In [ ]:

# First, ask the player about their CPU


cpuModel = str.lower(input("Please enter your CPU model: "))

# The match statement evaluates the variable's value


match cpuModel:
case "celeron": # We test for different values and print different messages
print ("Forget about it and play Minesweeper instead...")
case "core i3":
print ("Good luck with that ;)")
case "core i5":
print ("Yeah, you should be fine.")
case "core i7":
print ("Have fun!")
case "core i9":
print ("Our team designed nice loading screens… Too bad you won't see them...")
case _: # the underscore character is used as a catch-all.
print ("Is that even a thing?")
In [ ]:

You might also like