0% found this document useful (0 votes)
1 views9 pages

Python Session 5 Basic

The document contains a series of Python code snippets demonstrating various programming concepts such as variable assignment, arithmetic operations, input handling, and print formatting. It highlights common errors like NameError and explains the use of functions like print and input. The document also covers data type conversions and the use of eval for dynamic evaluation of expressions.

Uploaded by

Nazia Syed
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)
1 views9 pages

Python Session 5 Basic

The document contains a series of Python code snippets demonstrating various programming concepts such as variable assignment, arithmetic operations, input handling, and print formatting. It highlights common errors like NameError and explains the use of functions like print and input. The document also covers data type conversions and the use of eval for dynamic evaluation of expressions.

Uploaded by

Nazia Syed
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

In [ ]: - they never practice


- they will never listen 100%

- i did not undestand

- ds suitable for you

-

In [2]: number1=100
number2=200
number1 # 100 is not coming
number2 # 200 is latest so it is coming

Out[2]: 200

In [ ]: # in order to if you want to see both answers


# print

In [3]: number1=100 # 100 is saved in a variable number1


number2=200 # 200 is saved in a variable number2
print(number1) # print the value of number1 =100
print(number2) # print the value of number2=200

100
200

In [7]: print('hello') # step-1: hello


print('how') # step-2: how
print('are') # step-3: are
print('you') # step-4: you
print(hello) # step-5: hello is variable , is hello intoalised above
print(number1)

# python is a step - step
# Name error: Name hello not defined

hello
how
are
you

--------------------------------------------------------------------------
-
NameError Traceback (most recent call las
t)
Cell In[7], line 5
3 print('are') # step-3: are
4 print('you') # step-4: you
----> 5 print(hello) # step-5: hello is variable , is hello intoalised
above
6 print(number1)

NameError: name 'hello' is not defined


In [ ]: # you written 500 lines of code
# you got error at 200 line
# after 200 line, 201 to 500 will not execute

In [8]: print(number1,number2) # 100,200

100 200

In [9]: print(number1)
print(number2)

100
200

In [10]: print(number1)
print('-------')
print(200)
print('**********')

100
-------
200
**********

In [11]: print('hai','how','are','you')
print('hai how are you')

hai how are you


hai how are you

joining multiple print statements

In [12]: print('hai' , 'how')

hai how

𝑒𝑛𝑑
In [20]: print('hai',end='----->')
print('how')

hai----->how

In [28]: print('hai ',end='and')


print(' how')

hai and how


In [29]: print('how',end='')

how

In [31]: print('1',end=' ')


print('2',end=' ')
print('3')

1 2 3

In [38]: number1=200
number2=400
add=number1+number2
print("the addition of number1 and number2 is",add)

the addition of number1 and number2 is 60

In [41]: # the addition of 200 and 400 is 600


print("the addition of 20 and 40 is 60")

the addition of 20 and 40 is 60

In [44]: number1=2000
number2=4000
add=number1+number2
print("the addition of 20 and 40is",add)
# how add value is priniting automatically
# in the same way the value also shoudl print

the addition of 20 and 40is 6000

In [53]: num1=2000
num2=4000
add=num1+num2
print("the addition of",num1,"and",num2,"is",add)

the addition of 2000 and 4000 is 6000

In [ ]: # the addition of 20 and 40 is 60

In [61]: # Step-1: variable name ='python'


# step-2: city='Hyd'
# step-3 age=10

# my name is python, I came from hyd and Im 10 years old
name="python"
city="hyd"
age=10
print("my name is",name,'Im from',city,'and im',age,'old')

my name is python Im from hyd and im 10 old


In [65]: # my name is python, I came from hyd and Im 10 years old
name="python"
city="hyd"
age=10
print("my name is",name,'Im from',city,end=' and ')
print("im",age,'years old')

my name is python Im from hyd and im 10 years old

In [ ]: name='python'
city='hyd'
age=10
print('my name is ',name ,'i came from',city ,'i am',age ,'yers old')

In [ ]: num1=2000
num2=4000
add=num1+num2
print("the addition of",num1,"and",num2,"is",add)

In [ ]: # "the addition of 200 and 400 is 600"


# the addition of {} and {} is {}

In [67]: num1=200
num2=400
add=num1+num2
print("the addition of {} and {} is {}".format(num1,num2,add))

the addition of 200 and 400 is 600

In [70]: name='python'
city='hyd'
age=10
print("my name is {}, im from {} and my age is {} year old".format(age,city,

my name is 10, im from hyd and my age is python year old

In [ ]: # step-1: value1=20
# step-2: value2=30
# step-3: add=value1+value2
# step-4: sub=value1-value2
# step-5: mul=value1*value2
# step-6: div=value1/value2
#The addition of 20 and 30 is:50
#The subtraction 20 and 30 is: -10
#The multiplication of 20 and 30 is:600
#The division of 20 and 30 is : 0.66666
In [72]: val1 = 200
val2 = 300
add = val1+val2
sub = val1-val2
mul = val1*val2
div = round(val1/val2,2)
print('The addition of {} and {} is {}'.format(val1,val2,add))
print('The subtraction of {} and {} is {}'.format(val1,val2,sub))
print('The multiplication of {} and {} is {}'.format(val1,val2,mul))
print('The division of {} and {} is {}'.format(val1,val2,div))

The addition of 200 and 300 is 500


The subtraction of 200 and 300 is -100
The multiplication of 200 and 300 is 60000
The division of 200 and 300 is 0.67

In [75]: value1=20
value2=30
add=value1+value2
sub=value1-value2
mul=value1*value2
div=value1/value2
print("The addition of {} and {} is {}".format(value1,value2,add))
print("The Substraction of {} and {} is {}".format(value1,value2,sub))
print("The multiplication of {} and {} is {}".format(value1,value2,mul))
print("The division of {} and {} is {}".format(value1,value2,div))

The addition of 20 and 30 is 50


The Substraction of 20 and 30 is -10
The multiplication of 20 and 30 is 600
The division of 20 and 30 is 0.6666666666666666

In [78]: round(value1/value2,3)

Out[78]: 0.667

In [79]: 20/30 # / division

Out[79]: 0.6666666666666666

In [ ]: 20%30 # % modulus

In [ ]: 20//30 # floor division

In [80]: 5//2

Out[80]: 2

In [81]: 5%2

Out[81]: 1
In [82]: print(5/2) # division
print(5//2) # floor division = qutioent
print(5%2) # modulus = reminder

2.5
2
1

In [84]: val1 = 200 # manually i provide


val2 = 300 # i provided
add = val1+val2
print("The addition of {} and {} is {}".format(val1,val2,add))

The addition of 200 and 300 is 500

𝑖𝑛𝑝𝑢𝑡
In [93]: input()

300

Out[93]: '300'

In [94]: input("enter a number")

enter a number300

Out[94]: '300'

In [96]: input("enter some name:")

enter some name:apple

Out[96]: 'apple'

In [ ]: ​

In [ ]: name1='Apple'

In [101]: name1=input("what is A for:") # 'Apple'


name2=input("what is B for:")
print(name1)
print(name2)

what is A for:Apple
what is B for:Ball
Apple
Ball

In [99]: print(name1,name2)

Apple Ball
In [ ]: num1=60
num2=80
num1
num2

In [102]: num=input("enter number")

enter number10

In [103]: type(num)

Out[103]: str

In [104]: val1 = input("enter val1:") # '20'


val2 = input("enter val2:") # '30'
add = val1+val2 # '20'+'30'='2030' # 'A'+'B'='AB'
print("The addition of {} and {} is {}".format(val1,val2,add))

enter val1:20
enter val2:30
The addition of 20 and 30 is 2030

In [106]: val1,val2

Out[106]: ('20', '30')

In [107]: val1+val2

Out[107]: '2030'

In [108]: '20'+'30'

Out[108]: '2030'

In [110]: int(val1)

Out[110]: 20

In [111]: val1 = int(input("enter val1:")) # int('20')=20


val2 = int(input("enter val2:")) # int('30')=30
add = val1+val2 # 20+30=50
print("The addition of {} and {} is {}".format(val1,val2,add))

enter val1:20
enter val2:30
The addition of 20 and 30 is 50

In [ ]: val1 = input("enter val1:") # val1='20'


val2 = input("enter val2:") # val2='30'
add = int(val1)+int(val2) # int('20')+int('30') =20+30
print("The addition of {} and {} is {}".format(val1,val2,add))
In [112]: val1=int(input("Enter val1:"))
val2=int(input("Enter val2:"))
add=val1+val2

Enter val1:20
Enter val2:30

In [118]: num1=int(input("enter a number1:"))


num2=float(input("enter number2:"))
print(num1+num2)

enter a number1:200.5

--------------------------------------------------------------------------
-
ValueError Traceback (most recent call las
t)
Cell In[118], line 1
----> 1 num1=int(input("enter a number1:"))
2 num2=float(input("enter number2:"))
3 print(num1+num2)

ValueError: invalid literal for int() with base 10: '200.5'

In [ ]: int('200')--- works
int('200.5')--- fail
float('200')==works
float('200.5')==works

𝑒𝑣𝑎𝑙
In [121]: num1=eval(input("enter a number1:"))
num2=eval(input("enter number2:"))
print(num1+num2)

enter a number1:100
enter number2:100.5
200.5

In [123]: type(num2)

Out[123]: float

In [ ]: int('200.5') # fail

In [ ]: v1=input('enter v1:') # v1 str


v2=int(input('enter v2:')) # v2 int
v3=float(input('enter v3:')) # v3 float
v4=eval(input('enter v4')) # v4 === depends on provided value
In [127]: v4=eval(input('enter v4'))
print(type(v4))

enter v4apple

--------------------------------------------------------------------------
-
NameError Traceback (most recent call las
t)
Cell In[127], line 1
----> 1 v4=eval(input('enter v4'))
2 print(type(v4))

File <string>:1

NameError: name 'apple' is not defined

In [ ]: # Eval concept
val1 = 200
val2 = 300
add = val1+val2
sub = val1-val2
mul = val1*val2
div = round(val1/val2,2)
print('The addition of {} and {} is {}'.format(val1,val2,add))
print('The subtraction of {} and {} is {}'.format(val1,val2,sub))
print('The multiplication of {} and {} is {}'.format(val1,val2,mul))
print('The division of {} and {} is {}'.format(val1,val2,div))

In [ ]: # take three numbers


# a
# b
# c from the key board
# find the sum and average

In [ ]: ​

You might also like