Python With Naveen
Basic Python
Notes By
Naveen
Youtube : https://www.youtube.com/c/pythonwithnaveen
Facebook : https://www.facebook.com/groups/pythonwithnaveen/ Page 1
What is Python?
Python is an easy to learn, powerful programming language.
It has efficient high-level data structures and a simple but effective
approach to object-oriented programming.
Python’s elegant syntax and dynamic typing, together with its
interpreted nature, make it an ideal language for scripting and rapid
application development in many areas on most platforms.
Applications for Python
Python is used in many application domains. Here's a sampling.
The Python Package Index lists thousands of third
party modules for Python.
Web and Internet Development
Python offers many choices for web development:
Frameworks such as Django and Pyramid.
Micro-frameworks such as Flask and Bottle.
Advanced content management systems such
as Plone and django CMS.
Python's standard library supports many Internet protocols:
HTML , XML and JSON
E-mail processing.
Support for FTP, IMAP, and other Internet protocols.
Easy-to-use socket interface.
Python With Naveen
And the Package Index has yet more libraries:
Requests, a powerful HTTP client library.
BeautifulSoup, an HTML parser that can handle all sorts of
oddball HTML.
Feedparser for parsing RSS/Atom feeds.
Paramiko, implementing the SSH2 protocol.
Twisted Python, a framework for asynchronous
network programming.
Scientific and Numeric
Python is widely used in scientific and numeric computing:
SciPy is a collection of packages for mathematics, science, and
engineering.
Pandas is a data analysis and modeling library.
IPython is a powerful interactive shell that features easy editing
and recording of a work session, and supports visualizations
and parallel computing.
The Software Carpentry Course teaches basic skills for scientific
computing, running bootcamps and providing open-access
teaching materials.
Python With Naveen
Education
Python is a superb language for teaching programming, both at the
introductory level and in more advanced courses.
Books such as How to Think Like a Computer Scientist, Python
Programming: An Introduction to Computer Science,
and Practical Programming.
The Education Special Interest Group is a good place to discuss
teaching issues.
Desktop GUIs
The Tk GUI library is included with most binary distributions of
Python.
Some toolkits that are usable on several platforms are available
separately:
wxWidgets
Kivy, for writing multitouch applications.
Qt via pyqt or pyside
Platform-specific toolkits are also available:
GTK+
Microsoft Foundation Classes through the win32 extensions
Software Development
Python is often used as a support language for software developers,
for build control and management, testing, and in many other ways.
SCons for build control.
Python With Naveen
Buildbot and Apache Gump for automated continuous
compilation and testing.
Roundup or Trac for bug tracking and project management.
Business Applications
Python is also used to build ERP and e-commerce systems:
Odoo is an all-in-one management software that offers a range
of business applications that form a complete suite of
enterprise management applications.
Tryton is a three-tier high-level general purpose application
platform.
Automated Testing
Image Processing and OCR
Machine Learning Applications
Audio and Video Applications
Blockchain Applications
Artificial Intelligence
Data Science
Devops
etc., ....
Youtube : https://www.youtube.com/c/pythonwithnaveen
Facebook : https://www.facebook.com/groups/pythonwithnaveen/ Page 5
Python With Naveen
Python Download and Installation
Open https://www.python.org/ website in web browser.
Select "Downloads" and click on Python 3.8.0 Button.
Once you download you can see a icon like
Double click on the icon to install the python.
1st Screen
Note: Add Python3.8 to path check box must be checked. If this
option is not available simply click on Install Now.
Youtube : https://www.youtube.com/c/pythonwithnaveen
Facebook : https://www.facebook.com/groups/pythonwithnaveen/ Page 6
Python With Naveen
2nd Screen
3rd Screen
Congrats you have Installed Python Successful.
Youtube : https://www.youtube.com/c/pythonwithnaveen
Facebook : https://www.facebook.com/groups/pythonwithnaveen/ Page 7
Python With Naveen
Steps to Create a python and run
Note: I am Using Python IDLE
1. IDLE stands for Integrated Development and
Learning Environment or Language Environment.
2. In your computer open "start" menu and search for "IDLE" and
open.
3. In IDLE menu select "File" and "New File" and write the
program into the new file
Save the above file, (While saving we can give any name but
the extension must be ".py" only)
Note: Save the python files properly into a particular location.
4. ".py" stands for Python File.
5. To run the above program press "F5" (Function key from
keyboard)
Youtube : https://www.youtube.com/c/pythonwithnaveen
Facebook : https://www.facebook.com/groups/pythonwithnaveen/ Page 8
Python With Naveen
6. Output of the above program
Running the program using command prompt
1. Open the command prompt.
2. In command prompt move to python file location and type the
command as "python file_name.py" and press enter.
Note: If python path is not set you cannot run in the command
prompt.
Naming Styles
The table below outlines some of the common naming styles in
Python code and when you should use them:
Type Naming Convention Examples
Function Use a lowercase word or words. function,
Separate words by underscores to
my_function
improve readability.
Variable Use a lowercase single letter, word, x,
or words. Separate words with
var,
underscores to improve readability.
my_variable
Class Start each word with a capital Model,
letter. Do not separate words with
MyClass
underscores. This style is called
camel case.
Method Use a lowercase word or words. class_method,
Separate words with underscores to
method
improve readability.
Constant Use an uppercase single letter, CONSTANT,
word, or words. Separate words
MY_CONSTANT,
with underscores to improve
readability. MY_LONG_CONSTANT
Module Use a short, lowercase word or module.py,
words. Separate words with
my_module.py
underscores to improve readability.
Package Use a short, lowercase word or package,
words. Do not separate words with
mypackage
underscores.
Keywords
Keywords are reserved words by python do define the syntax of a
program.
In python as per 3.8 version we have 35 keywords.
Out of 35 keywords 3 keywords like False, True and None will begin
with capital letter and rest of the 32 keywords will begin with small
letter.
Note: Keywords may change from version to version.
To view list of Keywords in python IDLE type the program as,
import keyword
print(keyword.kwlist
)
save the above program and run it.
Identifier
Identifier is a name given to an entity like Class, Function (or)
Method and Variable.
To define an identifier we have to follow the below given rules.
1) Identifier must begin with an alphabet or an underscore ( _ ).
2) Identifier can contain
i) Capital or Small Alphabets ( a to z or A to Z)
ii) Digits from 0 to 9
iii) Only One special that is underscore ( _ ).
3) Identifier should not match with keywords.
4) Identifier should not be separated with white space.
Python With Naveen
Assignment
1) What is Identifier
2) What is the purpose of identifier
3) Can we declare variables/functions/classes without identifier
4) Can we declare Identifier with space
5) Can we declare identifier with starting character as ‘_’
6) Can we declare identifier with starting character as number
7) Can we declare keywords as identifier
8) Can we declare identifier with symbols
9) Can we declare 2 identifiers with same name
10) Identify In-valid identifier in the following list
Tell the below statements are valid identifiers are not.
11) customer name
12) salary
13) sub1_marks
14) _eid
15) 7bc
16) true
17) loanAmount
18) Else
19) no.of.seats
20) if
21) discount_Amount
22) gstAmt
23) calTotalMarks
24) Employee
25) Loan
26) interest-Amount
27) customer2
28) pid
Youtube : https://www.youtube.com/c/pythonwithnaveen
Facebook : https://www.facebook.com/groups/pythonwithnaveen/ Page 12
Python With Naveen
Solve these simple snippets
1. print(“Naveen Kumar”) 2. print(‘Sathya Technology’)
Output:- Output:-
3. print(“ Python ‘) 4. print(“Naveen Kumar\nPython”)
Output: Output:
5. print(“Core\tAdvanced”) 6. print(“Sathya@'Naveen' ”)
Output:- Output:-
7. print(“Sathya “Technology” ”) 8. print(“S@athya \”Tech\” ”)
Output:- Output:-
9. write code to get fallowing 10. Write code to get
Output fallowing Output
Hi Hello Students this is 'Naveen',
Students, This is Naveen, I am a "python" Faculty in "Sathya"
From Sathya Tech, AMPT. My Contact No is : '9052492329'
11. print(“Sathya”+ 12. print(5+2)
“Tech”) Output:- Output:-
14. print(“12” + “ 5”)
13. print( 12 – 6
Output:-
) Output:-
16. print(“Sathya”, “Tech”)
15. print(10/2)
Output:-
Output:-
print(“\n”,“Sathya”, “-”, “Tech”)
17. print(“Core”, “-”, “Pyhton”)
Output:-
Output:
18. print(“Python”+3) 19. print(3+3+
Output:- “Python”) Output:-
21. print(“sathya” – “Tech”)
20. print(“7” – 2
Output:
) Output:
23. print(“sathya\n\t\’Tech”)
22. print(“Sathya\n\tTech”)
Output:-
Output:-
25. print(“\t\tNaveen\tKumar”)
24. print(“** Sathya
print(“\t\t==========”)
Tech**) print(“\n ---
Output:
Python ---”)
output:-
Data types
Note: In python no need to write data type while declaring a
variable.
Variable
Variable is a named memory location which can store data
temporarily
Syntax:
variable_name = value
Example:
name = "Ravi"
salary = 185000.00
status = False
Important point
In Python variables are dynamic typed it means the type of a variable
is decided at run time.
In python we can declare multiple variables in one line.
In python we can assign multiple values to one variable.
In python a variable value can be any size not limit.
In python if multiple variables are holding same value will refer to
one object instead of creating a new object.
Python With Naveen
Assignment
1) What is variable
2) What is the purpose of variable
3) How to declare a variable
4) In python can we declare variable without assigning a value.
5) Is it possible to change variable value
6) a = 10
a = 20
What is the value of a = ?
7) bill = 250.50
discountAmount = 50
bill = bill + discountAmount
i) Identify number of variables in above Program
ii) Identify the Variable names in above program
iii) At end of Program what is the value of bill
8) Is it possible to declare more than one variable with same
name. Example
9) Do we follow any rules to declare variable name
10) Identify valid and In-valid declarations
i) x = 10 ii) a, b = 5, 10 iii) p, q, r = 2,6,9,7
iv) 25 = m
Youtube : https://www.youtube.com/c/pythonwithnaveen
Facebook : https://www.facebook.com/groups/pythonwithnaveen/ Page 16
Python With Naveen
v) p=5
q = 10
p+q=r
vi) x = 10
vii) age = 25
viii) gender = M
ix) rollno_ = 200
x) mno = 9052492329
xi) pin no=1234
xii) marks = 98.95
xiii) salary = 25478.35
xiv) pnr@no=1234567895
xv) email="[email protected]"
xvi) pnr_status=true
xvii) interest$rate = 8.2
xviii) creditcard1
xix) $aadharno
xx) break = 10
xxi) Break = 10
xxii) 3000 = x
Youtube : https://www.youtube.com/c/pythonwithnaveen
Facebook : https://www.facebook.com/groups/pythonwithnaveen/ Page 17
Python With Naveen
Solve these simple snippets
1. a = 5 2. a = 99.35
print("a") print(a)
Output:- Output:-
3. a = 5 4. a = 5
b=6 b=6
print("a+b") print("a"+b)
Output:- Output:-
5. a = 5 6. a = 15
b=6
b=6
c=a-b
print(a+b) print(c)
Ouptut:- Output:-
7. a = 5 8. a = 5
b=6 b=6
c=a+b c=a+b
print("Sum is : ",c) print(c,"is Your Result")
Output:- Output:-
9. a = 5 10.a = 5
b=6 b=6
c=a+b c=a+b
print("Sum ",c,"is your Result") print(a,"and",b,"sum is",c)
Output:- Output:-
11.cost = 2500 12.a = 10
discount = 10 b = 20
discAmt = (cost /100) * discount a=a+b
print(discAmt) b=a-b
a=a–b
Output:-
print(a, “\t”, b)
Output:-
13.a = 20 14.a = 2
a = "Naveen" b=a
print(a) print(b)
Output:- Output:-
15.n1 = "Naveen" 16.a = "7"
n2 = "with Python" b = "9"
fname= n1+"-"+n2 c=a+b
print(fname) print(c)
Output:- Output:-
17. a=7 2. a = 7
b=5 b=5
a=a*b c=a
b = a // b a=b
a = a // b b=c
print(a,"\t",b) print(a,"\t",b)
Output:- Output:-
3. n = 12 4. c1 = "Advance"
sum = 0 c2 = " Django"
r = n % 10 c3 = " Project"
n = n // 10 c4 = c1 + c3 + c2 + c3
sum = sum + r print(c4)
r = n % 10
Output:-
n = n // 10
sum = sum + r
print(n,"\t",sum)
Output:-
5. a = 5 6. p = 5
b= 7 q=3
a,b = b,a r=7
print(a,"\t",b) print(p,"\t",q,"\t",r)
Output:- Output:-
7. p = 5 8. p = 5
q=3 q=3
r=7 r=7
print(p,q,r) print(p,q,r,sep=",")
Output:- Output:-
9. p = 5 10.p = 5
q=3 q=3
r=7 r=7
print(p,q,r,sep="$") print(p,q,r,sep="-->")
Output:- Output:-
11.p = 5 12.p = 5
q=3 q=3
r=7 print(p,end="\t")
print(p,q,r,sep="V") print(q)
Output: Output:-
13.p = 5 14.p = 5
q=3 q=3
print(p) print(p,end="-")
print(q) print(q)
Output:- Output:-
15.a = 2.523649 16.a = 8
print(a) print("%d"%a)
Output:- Output:-
17.a = 8 18.a = 8
b = 4 print("%d%d"% b = 4 print("%d\t%d"%
(a,b)) (a,b))
Output:- Output:-
19.a = 41.756 20.a = 41.756
print("%f"%a) print("%.2f"%a)
Output:- Output:-
21.a = 41.756 22.a = 41.756
print("%.4f"%a) print("%d"%a)
Output:- Output:-
23.a = 6 24.a = 6
print("A value is :{0}".format(a)) b=3
print("{0}\t{1}".format(a,b))
Output:-
Output:-
25.a = 6 26.a = 6.43
b=3 print("a={0}".format(a))
print("a={0}\tb={1}".format(b,a)
)
Output:-
Output:-
Sample Programs:- Not Logical Operators
1. Consider a is 5 and b is 3, store a in b print a and b?
2. Consider a is 3 b is 2.5 print a and b?
3. Consider a is 2.3 , b is 5 store sum of a, b in c print c?
4. Consider a is "sathya" b is "tech" store fullname in c print c?
5. Consider a is 2, b is 5 store a and b in c then print c?
6. Consider a is 2.3 b is 9 store a in b print b?
7. Consider a is 5 b is 3 store sum of a, b in c, display o/p
8. Consider x is ‘a’ y is 5 store x in y print x, y?
9. Consider x is 2.3f y is 3 store sum of x, y in z print z?
10. Consider a is ‘x’ b is ‘y’ store a and b in c print c?
11. Consider x is 5 y is 2 x divide by y store, in z print z?
12. Consider x is 5 product of x and x store in y print y?
13. Consider x is 3 cube of x store in y print x, y?
14. Consider x is 3 square of x store in s, cube of x store in
c print s, c?
15. Consider x is 2, square of x store in s, cube of x store in c
Sum of s and c store in sum print sum?
16. Consider x is 5 , y is 3 store product of x and y in z and
sum of x, y, z store in p print p?
17. Consider principle value is 1000/- rate of interest is 15%
time period is 5years Display total amount based on simple
interest ?
18. Program to calculate total salary of
employee. Consider basic salary as 10,000
30% basic salary is hra.
10% basic salary is da.
19. Consider 3 subject marks as 60,70,80 Display total marks
and percentage
20. Product cost is 1000 RS.
gst is 10% of product cost.
calculate net price of a product.
21. Net price of a product is 230.
gst amount is 30
calculate original cost of product .
Python With Naveen
Operator will perform operation on operands.
Operators
Types of Operator's
1) Arithmetic Operators
Operator Operator Name Example
+ Addition I=4, J=2
>>>I+ J
>>>6
– Subtraction I=4, J=2
>>>I – J
>>>2
* Multiplication I=4, J=2
>>>I * J
>>> 8
/ Division I=30, J=20
>>>I /J
>>> 1.5
Youtube : https://www.youtube.com/c/pythonwithnaveen
Facebook : https://www.facebook.com/groups/pythonwithnaveen/ Page 24
Python With Naveen
% Modulus I=40, J=20
>>>I /J
>>> 0
** Exponent I=10, J=3
>>>I /J
>>> 1000
// Floor Division I=10, J=3
>>>I//J
>>> 3
2) Relational Operators
Operator Operator Name Example
== Equal to I = 20, J = 20
(I == J) is True
!= Not Equal to I = 20, J = 20
(I == J) is False
< Less than I = 40, J = 20
(I < J) is False
> Greater than I= 40, J = 20
(I > J) is True
<= Less than or equal to I = 40, J = 20
(I <= J) is False
>= Greater than or equal to I = 40, J = 20
(I >= J) is True
Youtube : https://www.youtube.com/c/pythonwithnaveen
Facebook : https://www.facebook.com/groups/pythonwithnaveen/ Page 25
Python With Naveen
3) Assignment Operator
Operator Operator Description Example
Name
= Assignment It assigns a value I = 40
from the right-side It assigns
operand to the left- 40 to I
side operand.
4) Shorthand Assignment Operator
Operator Operator Name Example
+= Add then assign I+=J
that means I = I + J
-= Subtract then assign I-=J
that means I = I – J
*= Multiply the assign I*=J
that means I = I * J
/= Divide then assign I/=J
that means I = I / J
%= Modulus then assign I%=J
that means I = I % J
**= Exponent then assign I**=J
that means I = I ** J
//= Floor division then assign I//=J
that means I = I // J
Youtube : https://www.youtube.com/c/pythonwithnaveen
Facebook : https://www.facebook.com/groups/pythonwithnaveen/ Page 26
Python With Naveen
5) Logical Operators
Operator Operator Name Example
and Logical AND 2<1 and 2<3
Result : False
or Logical OR 2<1 or 2<3
Result = True
not Logical NOT Not (5>4) Result = False
6) Membership Operators
Operator Example
in my_friends = ["Kapil","Bhanu","Srikanth","Naveen"]
"Bhanu" in my_friends# True
"vijay" in my_friends# False
not in my_friends = ["Kapil","Bhanu","Srikanth","Naveen"]
"vijay" not in my_friends # True
"Bhanu" not in my_friends # False
Youtube : https://www.youtube.com/c/pythonwithnaveen
Facebook : https://www.facebook.com/groups/pythonwithnaveen/ Page 27
Python With Naveen
7) Identity Operators in Python
Operator Example
isa not
is = 10 a = 10
b = 20 b = 20
c = 10 c = 10
a is b # Falsea aisisnot
c #b # True
True b is c # False
a is not c # False
b is not c # True
8) Bitwise Operators in Python
For instance, suppose there are two variables,
I = 10 and J = 20
And their binary values are:
I = 10 = 00001010
J = 20 = 00010100
Operator Operator Name Example
& Binary AND I&J
0000 0000
| Binary OR I|J
0001 1110
Youtube : https://www.youtube.com/c/pythonwithnaveen
Facebook : https://www.facebook.com/groups/pythonwithnaveen/ Page 28
Python With Naveen
^ Binary XOR I^J
0001 1110
~ Binary Complement ~I
1111 0101
<< Binary Left Shift I << 2
40 i.e. 1111 0000
>> Binary Right Shift I >> 2
15 i.e. 1111
Assignment
1. How many types of operators in python.
2. What is the difference between logical and 'and' bitwise and.
3. What is the difference between logical or and bitwise or.
4. What are identity Operators.
5. Output of print(9//2)
6. What is the type a when a = 1,00,000
7. What is the type of 'inf'?
a) Boolean
b) Integer
c) Float
d) Complex
Youtube : https://www.youtube.com/c/pythonwithnaveen
Facebook : https://www.facebook.com/groups/pythonwithnaveen/ Page 29
Python With Naveen
8. What does ~~~~~~5 evaluate to?
a) +5
b) -11
c) +11
d) -5
9. What is the purpose of not in operator.
10. What is the purpose pass statement in python.
11. Which function overloads the >> operator.
12. Output of "format(10/3)" and what is the return type.
13. Output of "Sathya" > "sathya".
14. Output of 10 and 10.
15. Output of 10 and 0.
16. Output of 0 and 10.
17. Output of 0 and 0.
18. Output of 0 or 10.
19. Which is the correct operator for power(xy)?
a) X^y
b) X**y
c) X^^y
d) None of the mentioned
20. Which one of these is floor division?
a) /
b) //
c) %
d) None of the mentioned
Youtube : https://www.youtube.com/c/pythonwithnaveen
Facebook : https://www.facebook.com/groups/pythonwithnaveen/ Page 30
Python With Naveen
21. What is the order of precedence in python?
i) Parentheses ii) Exponential iii) Multiplication iv) Division
v) Addition vi) Subtraction
a) i,ii,iii,iv,v,vi
b) ii,i,iii,iv,v,vi
c) ii,i,iv,iii,v,vi
d) i,ii,iii,iv,vi,v
22. Mathematical operations can be performed on a string.
State whether true or false.
a) True
b) False
23. Operators with the same precedence are evaluated
in which manner?
a) Left to Right
b) Right to Left
c) Can’t say
24. What is the output of this expression, 3*1**3?
a) 27
b) 9
c) 3
d) 1
25. The expression Int(x) implies that the variable x
is converted to integer. State whether true or false.
a) True
b) False
26. Which one of the following have the highest precedence
in the expression?
a) Exponential b) Addition c) Multiplication d) Parentheses
Youtube : https://www.youtube.com/c/pythonwithnaveen
Facebook : https://www.facebook.com/groups/pythonwithnaveen/ Page 31
Python With Naveen
27. Which of the following is invalid?
a) _a = 1
b) a = 1
c) str = 1
d) none of the mentioned
28. Which of the following is an invalid variable?
a) my_string_1
b) 1st_string
c) foo
d) _
29. Which of the following is not a keyword?
a) eval
b) assert
c) nonlocal
d) pass
30. Which of the following is an invalid statement?
a) abc = 1,000,000
b) a b c = 1000 2000 3000
c) a,b,c = 1000, 2000, 3000
d) a_b_c = 1,000,000
31. Which of the following cannot be a variable?
a) init
b) in
c) it
d) on
1. 2+3-5*6+7-9*3/2 11. What is the output?
a = "sathya"
2. 3*4-5/6+7-8*9+2-3*7
b = "Tech"
3. 6-7*8+9/5-8%3-7/2-3 c=a+b
4. 3*5-7+7*7-7/7+7%7 print(c)
12. What is the output?
5. 3>=5-6+7-8
s = "output is" + 2 + 3
6. 2>=4 and3==4 print(s)
7. 2>=4 or 3==4 13. What is the output?
s =2 +3+5*6+7-3*5//6
8. What is the output?
print(s)
i=5; j=6; i=i+j; j=j-i; 14. What is the output?
print(i); b = 5+2-3*5+7/2-3/4>6-
5+4*3/9+9-2/3
print(j);
print(b)
9. What is the output? 15. What is the output?
i=2*3+5*6//7-3//2+7*6//3 i=7
j=3
j=i-3*4-5*6+7-3*4+7 b=i>=j
i=i+j; j=j-i*3/2; i=i*7/-2+5; print(b)
j=j-3*4%7; 16. What is the output?
print(2+3-5*6+7-8)
print(i); print(j); print(5**6); print(5*6)
10. What is the output of below print(2+4)
code snippet? print(2+3.5)
print((2>3))
i = 3 * 4 - 6 + 7 // 2 + 4;
print(12//5+12%5)
j = 3 * 5 - 6 // 7 + 8 % 9 - 3 * 5 + 7; 17. 3>4 and 3<5
i = i + j; j = j - i; 18. 5+3*7-9>4+5*6 and 4<5
19. 4**4
print(i); print(j); 20. 6*2-3/4+4-3//2
Reading input From Keyboard
To read input from keyboard we use "input" function. The "input"
function will read any given input from keyboard and it will return in
string type.
# Write a script to Read name from User
x = input("Enter Your Name ")
print(x)
print(type(x))
# WAP to Read 2 no's from user and print sum of the
2 nos.
no1 = input("1st No ")
no2 = input("2nd No ")
# The input function is returning string so we
are adding 2 Strings not int's
print("The sum = ",no1+no2)
Python With Naveen
Type Conversion
The process of converting a data type into another data type is
known as type conversion.
Function Description
int( ) It converts to an integer
float( ) It converts to a floating-point number.
complex( ) It creates to a complex number.
str( ) It converts to a string.
tuple( ) It converts to a tuple.
list( ) It converts to a list.
set( ) It converts to a set.
dict(y) It creates a dictionary and y should be a sequence of
(key,value) tuples.
ord( ) It converts a character into an integer.
hex(y) It converts an integer to a hexadecimal string.
oct(y) It converts an integer to an octal string
chr( ) It converts ASCII value to a character.
no1 = input("1st No :")
no2 = input("2nd No :")
# Converting Str to int
a = int(no1) b
= int(no2)
print("After Converting Sum = ",a+b)
Youtube : https://www.youtube.com/c/pythonwithnaveen
Facebook : https://www.facebook.com/groups/pythonwithnaveen/ Page 35
Python With Naveen
Above program in short.
no1 = int(input("1st No :")) no2
= int(input("2nd No :"))
print("Sum = ",no1+no2)
Above program in 1 line.
print("The sum = ",int(input("1st No :")) +
int(input("2nd No :")))
# Reading 2 no's from keyboard and converting into
float
no1 = float(input("1st No :")) no2
= float(input("2nd No :"))
print("Sum = ",no1+no2)
# Legacy printing
x = 256.641484135541
print("Python-Lang Format :",x)
print("C-Lang Format %f"%x)
print("C-Lang Format %0.f"%x)
print("C-Lang Format %0.1f"%x)
print("C-Lang Format %0.2f"%x)
print("C-Lang Format %0.3f"%x)
print("C-Lang Format %0.4f"%x)
print("C-Lang Format %0.5f"%x)
print("C-Lang Format %0.6f"%x)
Youtube : https://www.youtube.com/c/pythonwithnaveen
Facebook : https://www.facebook.com/groups/pythonwithnaveen/ Page 36
Python With Naveen
print("C-Lang Format %0.7f"%x)
print("C-Lang Format %0.8f"%x)
print("C-Lang Format %0.9f"%x)
print("C-Lang Format %0.10f"%x)
To read any input from keyboard we use eval()
This function will read int, float, str, bool, list, set, tuple, dictionary
x = eval(input(" Enter any type of Value :")) print(x)
print(type(x))
# WAP to accept two numbers and perform
addition, Subtraction, Multiplication and Division
no1 = int(input("Enter 1st no :")) no2 =
int(input("Enter 2nd no :")) print("The
Add = ",no1+no2) print("The Sub =
",no1-no2) print("The Mul = ",no1*no2)
print("The Div = ",no1/no2)
# WAP to find the type Of a Variable
x = eval(input("You can Enter any type of
Value :"))
print(x) print(type(x))
Youtube : https://www.youtube.com/c/pythonwithnaveen
Facebook : https://www.facebook.com/groups/pythonwithnaveen/ Page 37
Python With Naveen
Assignment
1. How to read data dynamically
2. What is the default data type for input( )
3. Is it possible to convert String type to int type
4. How to convert string data into int type
5. How to convert string data into float type
6. What is the purpose of eval( )
7. WAP to Display Welcome To Sathya Technologies
8. WAP to accept the Student Name and Display a message
Welcome Student Name
9. WAP to accept two numbers and perform
addition, Subtraction, Multiplication and Division
10. WAP to find the type Of a Variable
11. WAP to accept student first name and last name and
Display the student full Name
12. WAP to accept a str value and convert the string value
to int and print int value.
13. WAP to accept a str value and convert the str value
to float and print double value.
14. WAP to accept student no, student name, marks1,
marks2, marks3 and calculate total marks, average marks
and print.
15. WAP to input two numbers and swap those values.
16. Sunitha Went to market, Purchased 4 sim cards. Read sim
cost dynamically then calculate and display total bill.
Youtube : https://www.youtube.com/c/pythonwithnaveen
Facebook : https://www.facebook.com/groups/pythonwithnaveen/ Page 38
Python With Naveen
17. Mr.Ravi went to shopping mall, purchased 2 products Read
product cost dynamically then Calculate total bill amount.
18. Mr Advaith Went to CTC, purchased laptop, Read laptop
cost dynamically, In CTC for every item 15% discount is
available. Now display bill Amount, discount amount and
total bill.
19. Read Lakshmi basic salary dynamically, Company provides
ta,da,hra based on basic salary Ta is 10% , Da is 8 % Hra is 12%
Now calculate total monthly salary of Lakshmi
20. Case: One acre of land is equivalent to 43,560 square feet.
Write a program that asks the user to enter the total square
feet s and find number of acres. And vice versa
Solve these snippet's
1. a = input() 2. a = input()
print(a) print(a)
Output:- print(type(a))
Output:-
3. a = input("Enter fname: ") 4. a = input("Enter any no:
b = input("Enter lname: ") ") b = input("Enter any no:
c=a+b ") c = a + b
print(c) print(c)
Output : Output:-
-
5. a = int(input("Any no : "))
Python 6. a = float(input("Any no : "))
With Naveen
print(a) print(a)
print(type(a)) print(type(a))
Output:- Output:-
7. a = int(input("Any no : ")) 8. a = float(input("Any no : "))
print(a) print(a)
print(type(a)) print(type(a))
Output:- Any no : 2.5 Output:- Any no : 5
9. a = eval(input("Any no : ")) 10. a = eval(input("Any no : "))
print(a) print(a)
print(type(a)) print(type(a))
Output:- Any no : 2 Output:- Any no : 5.4
a = chr(int(input("Enter any 12. a = chr(int(input("Enter any
no : "))) no : ")))
print(a) print(a)
Output:- Enter any no : 65 Output:- Enter any no : 97
Expression O/p Expression O/p
int(123.654) str(10.5)
int(False) str(True)
int("10") str(False)
int("10.5") z = float("3")
int("ten") print(10 > 9)
float(10) print(10 == 9)
float(True) float("ten")
float(False) bool(0)
float("10") bool(1)
float("10.5") bool(10)