Python
What is Python
• Python is a dynamically typed, General
Purpose Programming Language that supports
an object-oriented programming approach as
well as a functional programming approach.
• Python is also an interpreted and high-level
programming language.
• It was created by Guido Van Rossum in 1989.
Features of Python
• Python is simple and easy to understand.
• It is Interpreted and platform-independent which
makes debugging very easy.
• Python is an open-source programming language.
• Python provides very big library support. Some of
the popular libraries include NumPy, Tensorflow,
Selenium, OpenCV, etc.
• It is possible to integrate other programming
languages within python.
What is Python used for
• Python is used in Data Visualization to create plots and
graphical representations.
• Python helps in Data Analytics to analyze and understand
raw data for insights and trends.
• It is used in AI and Machine Learning to simulate human
behavior and to learn from past data without hard coding.
• It is used to create web applications.
• It can be used to handle databases.
• It is used in business and accounting to perform complex
mathematical operations along with quantitative and
qualitative analysis.
Installation & Getting Started
• Steps to Install Python:
• Visit the official python website:
https://www.python.org/
• Download the executable file based on your
Operating System and version specifications.
• Run the executable file and complete the
installation process.
Starting Python:
• Open Python IDE or any other text editor of
your preferred choice. Let’s understand
python code execution with the simplest print
statement. Type the following in the IDE:
• print("Hello World !!!")
• Now save the file with a .py extension and Run
it. You will get the following output:
• Hello World !!!
Python Comments
• Single-Line Comments:
• To write a comment just add a ‘#’ at the start
of the line.
• Example 1:
• #This is a 'Single-Line Comment' print("This is
a print statement.")
Python Comments
• Multi-Line Comments:
• To write multi-line comments you can use ‘#’ at each line or you
can use the multiline string.
• Example 1: The use of ‘#’.
• #It will execute a block of code if a specified condition is true. #If
the codition is false than it will execute another block of code.
• p=7
• if (p > 5):
• print("p is greater than 5.")
• else: print("p is not greater than 5.")
Multi-Line Comments:
Python Variables
• Variables are containers that store information
that can be manipulated and referenced later
by the programmer within the code.
• In python, the programmer does not need to
declare the variable type explicitly, we just
need to assign the value to the variable.
name = "Abhishek" #type str
age = 20 #type int
passed = True #type bool
Python Variables
• It is always advisable to keep
variable names descriptive and
to follow a set of conventions
while creating variables:
• Variable name can only contain
alpha-numeric characters and
underscores (A-z, 0-9, and _ )
• Variable name must start with a
letter or the underscore
character.
• Variables are case sensitive.
• Variable name cannot start with
a number.
Scope of variable:
• The scope of the variable is the area within
which the variable has been created. Based on
this a variable can either have a local scope or
a global scope.
• Local Variable
• Global Variable
Local Variable:
• A local variable is
created within a
function and can be
only used inside that
function. Such a
variable has a local
scope.
• Example:
Data Types
• Data type specifies the type of value a variable requires to do
various operations without causing an error. By default, python
provides the following built-in data types:
• Numeric data: int, float, complex
• int: 3, -8, 0
• float: 7.349, -9.0, 0.0000001
• complex: 6 + 2i
• more on numeric data types in the number chapter.
• Text data: str
• str: “Hello World!!!”, “Python Programming”
• Boolean data:
• Boolean data consists of values True or False.
Sequenced data: list, tuple, range
• list: A list is an ordered collection of data with
elements separated by a comma and enclosed
within square brackets. Lists are mutable and
can be modified after creation.
• Example:
• list1 = [8, 2.3, [-4, 5], ["apple", "banana"]]
print(list1)
• output: [8, 2.3, [-4, 5], ['apple', 'banana']]
• tuple: A tuple is an ordered collection of data with
elements separated by a comma and enclosed
within parentheses. Tuples are immutable and can
not be modified after creation.
• Example:
• tuple1 = (("parrot", "sparrow"), ("Lion", "Tiger"))
print(tuple1)Copy
• Output:
(('parrot', 'sparrow'), ('Lion', 'Tiger'))
• range: returns a sequence of numbers as specified by the user. If not specified by the
user then it starts from 0 by default and increments by 1.
• Example:
• sequence1 = range(4,14,2)
• for i in sequence1:
• print(i)
• Output:
• 4
• 6
• 8
• 10
• 12