Python Unit 1-Pshr
Python Unit 1-Pshr
II B.Tech I Semester
Python Programming
Faculty
Dr. P. Srihari Reddy
Professor
What is a program ?
System software
Programming software
Application software
System software
Device drivers, operating systems (OS), compilers, disk formatters, text
editors
It is responsible for managing hardware components.
Programming software
Tools to aid developers in writing programs.
The various tools available are compilers, linkers, debuggers, interpreters
Application software
HighLanguage
High-Level HighLanguage
Low-Level
Interpreter
An interpreter is a program that reads source code one statement at a time,
translates the statement into machine language, executes the machine
language statement, then continues with the next statement.
At National Research Institute for Mathematics and Computer Science in Netherland
Thrust Areas of Python
Academia
accelerated rate and is competing with MATLAB for the title of most
NumPy: NumPy stands for Numerical Python. It is a Python library used for working
with arrays. It also has functions for working in domain of linear algebra, fourier
transform, and matrices.
Sympy: SymPy stands for Symbolic Mathematics in Python and is a Python library
for dealing with mathematics.
Matplotlib: Matplotlib is a cross-platform, data visualization and graphical plotting
library for Python
• Most of these tools are available under Berkeley Software Distribution (BSD) license
and can be used without any restrictions
Machine Learning
• Scikit- Learn is a well-known Machine Learning tool built on top of other
Python scientific tools like NumPy, SciPy and Matplotlib. This allows Scikit-
• Each of the data stored in series is labeled after the index. DataFrame is a
tabular data structure with labeled rows and columns similar to Excel
spreadsheet
Open Source Software
• The term “Open Source” refers to something people can modify and share
because its design is publicly accessible.
• Open source software is software with source code that anyone can
inspect, modify, and enhance.
• Programmers who have access to a computer program’s source code can
improve that program by adding features to it or fixing parts that don’t
always work correctly
Popularity of Python
The biggest strength of Python is huge collection of standard library which can
be used for the following:
Machine Learning
Data Science
GUI Applications
Web frameworks like Django (used by YouTube, Instagram, Dropbox)
Multimedia
Scientific computing
Windows 10 OS.
Step 1: Go to the link https://www.anaconda.com/products/distribution and
click on download
Step 2: Click on the executable file of Anaconda Python distribution which you
have downloaded and the setup screen will start loading.
Step 3: You will get a welcome screen as shown in FIGURE.
Step 4: You will get a License Agreement Screen, read the licensing terms and click on
I Agree button.
Step 5: Assuming that you are the only user on your system, select Just Me radio
button as shown in FIGURE.
Step 6: You need to choose a location to install Anaconda Python distribution. The default
installation will be under Users folder. Change the destination folder to C:\Anaconda3 to
install Anaconda and click on Next button FIGURE .
Step 7: click Next and Finish the Installation.
Python Basics : Identifiers
• An identifier is a name given to a variable, function, class or module.
Identifiers may be one or more characters in the following format:
• The equal sign (=) also known as simple assignment operator is used to
assign values to variables.
• Simple assignment is done with the equal sign (=) and simply
assigns the value of its right operand to the variable on the left. For
example,
1. >>> x = 5
2. >>> x = x + 1
3. >>> x
6
Comparison Operators
• When the values of two operands are to be
compared then comparison operators are used.
• A string can also contain spaces. You can use single quotes or
double quotes to represent strings and it is also called a string
literal.
• Another way of doing this is to use triple quotes, either ''' or """. These
triple quotes are generally used for multiline strings. However, they
can be used as a multiline comment as well. For example,
'''This is
multiline comment
in Python using triple quotes'''
Reading Input
• In Python, input() function is used to gather data from the user. The
syntax for input function is,
variable_name = input(prompt)
Output
Which country do you live in? India
I live in India
Program: Given the Radius, Write Python Program
to Find the Area and Circumference of a Circle
1. radius = int(input("Enter the radius of a circle"))
2. area_of_a_circle = 3.1415 * radius * radius
3. circumference_of_a_circle = 2 * 3.1415 * radius
4. print(f"Area = {area_of_a_circle} and
Circumference = {circumference_of_a_circle}")
Output
Enter the radius of a circle 5
Area = 78.53750000000001 and Circumference =
31.415000000000003
Type Conversions
• You can explicitly cast, or convert, a variable from one type
to another.
• The int() Function
• To explicitly convert a float number or a string to an integer,
cast the number using int() function.
Output
Equivalent Character for ASCII value of 100 is d
The complex() Function
• Use complex() function to print a complex number
with the value real + imag*j or convert a string or
number to a complex number.
1. complex_with_string = complex("1")
2. complex_with_number = complex(5, 8)
3.print(f"Result after using string in real part
{complex_with_string}")
4. print(f"Result after using numbers in real and imaginary
part {complex_with_ number}")
Output
Result after using string in real part (1+0j)
Result after using numbers in real and imaginary part (5+8j)
The ord() Function
The ord() function returns an integer representing Unicode code point for the given
Unicode character.
Program: Program to Demonstrate ord() Casting Function
1. unicode_for_integer = ord('4')
2. unicode_for_alphabet = ord("Z")
3. unicode_for_character = ord("#")
4. print(f"Unicode code point for integer value of 4 is {unicode_for_integer}")
5. print(f"Unicode code point for alphabet 'A' is {unicode_for_alphabet}")
6. print(f"Unicode code point for character '$' is {unicode_for_character}")
Output
Unicode code point for integer value of 4 is 52
Unicode code point for alphabet 'A' is 90
Unicode code point for character '$' is 35
The hex() Function
Convert an integer number (of any size) to a lowercase
hexadecimal string prefixed with “0x” using hex() function.
Output
After Integer to Hex Casting the result is 0xff
The oct() Function
Convert an integer number (of any size) to an octal string
prefixed with “0o” using oct() function.
Output
After Integer to Hex Casting the result is 0o377
The type() Function and Is Operator
The syntax for type() function is, type(object)
The type() function returns the data type of the given object.
1. >>> type(1)
<class 'int'>
2. >>> type(6.4)
<class 'float'>
3. >>> type("A")
<class 'str'>
4. >>> type(True)
<class 'bool'>
The type() function comes in handy if you forget the type of variable or
an object during the course of writing programs.
• The operators is and is not are identity operators.
Operator is evaluates to True if the values of operands
on either side of the operator point to the same object
and False otherwise.
1. >>> x = "Seattle"
2. >>> y = "Seattle"
3. >>> x is y
True
Dynamic and Strongly Typed Language