Python Unit I
Python Unit I
UNIT – I
1.1. BASICS OF PYTHON PROGRAMMING
Python is a popular programming language. Python is a versatile and high-level
programming language. It was created by Guido van Rossum in 1991. It’s simple to use,
packed with features and supported by a wide range of libraries and frameworks. Its clean
syntax makes it beginner-friendly.
Python offers a wide range of functionalities, easy readability and simplicity that
makes various domains such as web development, data science, artificial intelligence,
automation and more. It is used for:
Python can be used on a server to create web applications
Python can be used software to create workflows
Python can connect to database systems. It can also read and modify files
Python can be used to handle big data and perform complex mathematics
Python can be used for rapid prototyping and system scripting
a. Why Python?
Python works on different platforms (Windows, Mac, Linux, etc)
Python has a simple syntax similar to the English language
Python has syntax that allows developers to write programs with fewer lines than
some other programming languages
Python runs on an interpreter system. This means that prototyping can be very
quick
Python can be treated in a procedural way, an object-oriented way or a functional
way
The python interpreter’s line-editing features include interactive editing, history
substitution and code completion on systems that support the GNU Readline library
It is possible to write Python in an Integrated Development Environment
b. Advantages
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 requires fewer lines of code compared to other programming languages
Python provides many job opportunities in Software Development, Data Science
and AI/ML
Python provides popular Web Development, Data Science and Data Analysis
Libraries like Django, Flask, Pandas, Tensorflow, Scikit-learn and many more
a. Types of Literals
Numeric Literals
String Literals
Boolean Literals
Collection Literals
Special Literals
1. Numeric Literals
Numeric literals represent numbers and are classified into three types:
Integer Literals – This represents the whole numbers (positive, negative or zero) without a
decimal point. Example: 10, -25 and 0
Integer literals consists of three different types of literal values decimal, octal and
hexadecimal literals.
Decimal Literal - Decimal literals represent the signed or unsigned numbers.
Digitals from 0 to 9 are used to create a decimal literal value. The following
statement assigning decimal literal to the variable:
x = 10, y = -25 and z = 0
Octal Literal - Python allows an integer to be represented as an octal number. A
numeric representation with only eight digit symbols (0 to 7) but prefixed by 0o or
0O is an octal number in Python. The following statement assigning octal literal to
the variable: x = 0O34
“””
Operator Description
() Parentheses
Each type of expression takes certain types of operands and uses a specific set of
operators. Evaluation of a particular expression produces a specific value. Python supports
various types of expressions including:
Constant Expressions
Arithmetic Expressions
Integral Expressions
Floating Expressions
Comparison expressions
Logical Expressions
Bitwise Expressions
Combinational Expressions
String Expressions
Assignment Expressions
Conditional Expressions
Lambda Expressions
Generator Expressions
Regular Expressions
a. Constant Expressions - These are the expressions that have constant values only
Example
x = 15 + 1.3
print(x)
Output
16.3
Key Features
Multi-dimensional support - NumPy arrays can handle more than one dimension
and making suitable for matrix operations and more complex mathematical
constructs
Broad broadcasting capabilities - They can perform operations between arrays
of different sizes and shapes. So this feature is known as broadcasting
Efficient storage and processing - NumPy arrays are stored more efficiently than
Python lists and provide optimized performance for numerical operations
Example #1
import array as arr
# creating array of integers
a = arr.array('i', [1, 2, 3])
# accessing First Araay
print(a[0])