0% found this document useful (0 votes)
4 views73 pages

1) Introduction to Python

The document provides an introduction to Python programming, detailing its history, features, and data types. It covers the evolution of Python from its inception by Guido van Rossum in 1989 to its current versions, highlighting its applications in various fields such as data analysis and artificial intelligence. Additionally, it explains Python's syntax, variable creation, comments, and operators, making it a comprehensive guide for beginners and experienced developers alike.

Uploaded by

ashtron811
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)
4 views73 pages

1) Introduction to Python

The document provides an introduction to Python programming, detailing its history, features, and data types. It covers the evolution of Python from its inception by Guido van Rossum in 1989 to its current versions, highlighting its applications in various fields such as data analysis and artificial intelligence. Additionally, it explains Python's syntax, variable creation, comments, and operators, making it a comprehensive guide for beginners and experienced developers alike.

Uploaded by

ashtron811
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
You are on page 1/ 73

Introduction to Python Programming

By
Dr. Yogesh Rajput
Introduction to Python

• Python is a popular programming language.

• It was created by Guido van Rossum, and released in 1991.

• It is used for:
– Data Analysis

– Artificial Intelligence

– Scientific Computing, etc.

Dr. Yogesh M. Rajput 2


History

Dr. Yogesh M. Rajput 3


History

• The history of Python dates back to the


late 1980s when a Dutch programmer
named Guido van Rossum started
working on the language as a side
project during Christmas in December
1989.

Dr. Yogesh M. Rajput 4


History

• He was working at the CWI industry in


the Netherlands, and the project was
initially intended as a successor to the
ABC programming language, which
was used for teaching programming
concepts.

Dr. Yogesh M. Rajput 5


Development and Early Releases

• In February 1991, Guido van Rossum


released the first version of Python (0.9.0)
as an open-source project.

• The name "Python" was inspired by the


British comedy series "Monty Python's
Flying Circus," of which Guido was a fan.

Dr. Yogesh M. Rajput 6


Development and Early Releases

• Python's design was heavily influenced by


ABC, emphasizing readability and
simplicity.

• The goal was to create a language that was


easy to learn and could be used for various
purposes.

Dr. Yogesh M. Rajput 7


Python 1.0

• Python 1.0 was released in January 1994


and included many features that are still
part of Python's core today, such as

• lambda functions, map(), filter(), and


reduce() functions.

Dr. Yogesh M. Rajput 8


Python 2.x Series
• Python 2.0 was released in October 2000, introducing list
comprehensions and a garbage collector.
• Subsequent versions in the 2.x series brought various improvements
and optimizations.
• Python 2.7, released in July 2010, marked the end of the 2.x series.
• It remained widely used even after the release of Python 3.x due to
compatibility concerns.

Dr. Yogesh M. Rajput 9


Python 3.x Series

• Python 3.0, a major advancement of the language, was released in


December 2008.

• This version aimed to clean up inconsistencies and introduce new


features while maintaining backward compatibility as much as
possible.

Dr. Yogesh M. Rajput 10


Python 3.x Series
• However, the changes in Python 3.x were significant enough to
break compatibility with some existing Python 2.x code.

• This led to a slow adoption of Python 3.x in the early years.

• Over time, the community and library maintainers have been


updating their code to be compatible with Python 3.x, leading
to a broader adoption of Python 3.x in recent years.

Dr. Yogesh M. Rajput 11


Continuous Development and Popularity
• Python has continued to evolve rapidly, with regular releases
introducing new features and improvements.

• The language's simplicity, versatility, and community support


have contributed to its growing popularity in various fields,
including web development, data science, machine
learning, and more.

Dr. Yogesh M. Rajput 12


Why Python?

Dr. Yogesh M. Rajput 13


Why Python?

• Python works on different platforms (Windows, Mac, Linux,


Raspberry Pi, etc).

• Python has a simple syntax similar to the English language.

Dr. Yogesh M. Rajput 14


Why Python?

• Python has syntax that allows developers to write programs


with fewer lines than some other programming languages.

• Python runs on an interpreter system, meaning that code can


be executed as soon as it is written.

Dr. Yogesh M. Rajput 15


Features of Python Programming

Dr. Yogesh M. Rajput 16


Simple and Readable Syntax
• Python's syntax is designed to be clean
and easily readable, making it an
excellent choice for beginners and
experienced developers alike.

Dr. Yogesh M. Rajput 17


Interpreted Language
• Python is an interpreted language,
which means that code execution
happens line-by-line without the need
for explicit compilation.

• This makes development faster and


promotes rapid prototyping.

Dr. Yogesh M. Rajput 18


Memory Management
• Python has automatic memory management
through garbage collection.

• Developers do not need to manually


allocate or deallocate memory, reducing
the risk of memory-related bugs.

Dr. Yogesh M. Rajput 19


High-Level Language
• Python abstracts away low-
level details, enabling
developers to focus on problem-
solving rather than memory
management or other technical
complexities.

Dr. Yogesh M. Rajput 20


Large Standard Library
• Python comes with an extensive standard library that
includes modules and packages for various tasks, such as
working with files, regular expressions, networking, data
manipulation, and more.

• This rich library allows developers to control existing


functionalities and saves time in coding.

Dr. Yogesh M. Rajput 21


Object-Oriented Programming (OOP)
• Python supports object-oriented
programming, allowing developers to
create and use classes and objects,
leading to reusable and modular
code.

Dr. Yogesh M. Rajput 22


Extensible
• Python can be extended with
modules and libraries written in
other languages, making it
highly extensible and adaptable
to various use cases.

Dr. Yogesh M. Rajput 23


Cross-Platform
• Python is a cross-platform language, meaning code written
on one operating system can run on other platforms with
little to no modification.

Dr. Yogesh M. Rajput 24


Third-Party Libraries
• Python has a vast collection of third-party libraries and
frameworks for specific tasks like web development (Django,
Flask), data analysis (Pandas, NumPy), scientific computing
(SciPy), machine learning (TensorFlow, PyTorch), and more.

• These libraries help developers save time and effort by


providing ready-made solutions.

Dr. Yogesh M. Rajput 25


Creating Variables

Dr. Yogesh M. Rajput 26


Creating Variables
• In Python, a variable is a name that represents a memory
location to store data.

• It acts as a container to hold values, such as numbers,


strings, lists, or other data types.

• Variables allow you to assign a name to a value, making it


easier to reference and manipulate data within a program.

Dr. Yogesh M. Rajput 27


Creating Variables
• Variable names must start with a letter (a-z, A-Z) or an underscore (_).

• The rest of the name can contain letters, numbers, and underscores.

• Variable names are case-sensitive, meaning "my_variable" and


"My_Variable" are considered different variables.

• Python reserved keywords (e.g., if, else, for, while, etc.) cannot be used
as variable names.

Dr. Yogesh M. Rajput 28


Creating Variables

• Python has no command for


x=5
declaring a variable.
y = “Sham"
• A variable is created the
print(x)
moment you first assign a value
to it.
print(y)

Dr. Yogesh M. Rajput 29


Get the Type

• You can get the data type of a


variable with the type() x = 5

function. y = “Sham"
print(type(x))
print(type(y))

Dr. Yogesh M. Rajput 30


Python Comments

Dr. Yogesh M. Rajput 31


Python Comments
• Comments can be used to explain Python code.

• Comments can be used to make the code more readable.

• Comments can be used to prevent execution when testing code.

• Comments starts with a #, and Python will ignore them:

Dr. Yogesh M. Rajput 32


Python Comments

#This is a comment
print(“Welcome to SIU!")

print(“Welcome to SIG!") #This is a comment

Dr. Yogesh M. Rajput 33


Multiline Comments
• Python does not really have a syntax for multiline comments.

• To add a multiline comment you could insert a # for each line

#This is a comment
#written in
#more than just one line
print("Hello, World!")

Dr. Yogesh M. Rajput 34


Multiline Comments
• Since Python will ignore string literals that are not assigned to
a variable, you can add a multiline string (triple quotes) in
your code, and place your comment inside it:
"""
This is a comment
written in
more than just one line
"""
print("Hello, World!")
Dr. Yogesh M. Rajput 35
Data Types

Dr. Yogesh M. Rajput 36


Built-in Data Types
• In programming, data type is an important concept.

• Variables can store data of different types, and different types


can do different things.

• Python has the following data types built-in by default, in these


categories

Dr. Yogesh M. Rajput 37


Built-in Data Types
Text Type: str
Numeric Types: int, float, complex
Sequence Types: list, tuple, range
Mapping Type: dict
Set Types: set
Boolean Type: bool
Binary Types: bytes, bytearray
None Type: NoneType

Dr. Yogesh M. Rajput 38


Numeric Types

Dr. Yogesh M. Rajput 39


Numeric Types
• There are three numeric types in Python:
– int

– float

– complex

• Variables of numeric types are created when you assign a value


to them.

Dr. Yogesh M. Rajput 40


Numeric Types
x = 1 # int
y = 2.8 # float
z = 1j # complex

• To verify the type of any object in Python, use the type() function:

print(type(x))
print(type(y))
print(type(z))

Dr. Yogesh M. Rajput 41


1) int
• int, or integer, is a whole number, positive or negative, without
decimals, of unlimited length.
x = 1
y = 35656222554887711
z = -3255522

print(type(x))
print(type(y))
print(type(z))

Dr. Yogesh M. Rajput 42


2) float
• float, or "floating point number" is a number, positive or
negative, containing one or more decimals.
x = 1.10
y = 1.0
z = -35.59

print(type(x))
print(type(y))
print(type(z))

Dr. Yogesh M. Rajput 43


2) float
• float can also be scientific numbers with an "e" to indicate the
power of 10.
x = 35e3
y = 12E4
z = -87.7e100

print(type(x))
print(type(y))
print(type(z))

Dr. Yogesh M. Rajput 44


3) Complex
• We can use complex numbers to perform mathematical
operations involving imaginary numbers

• Complex numbers are written with a "j" as the imaginary part:

x = 3+5j
y = 5j
z = -5j

print(type(x))
print(type(y))
print(type(z))
Dr. Yogesh M. Rajput 45
Python Casting

Dr. Yogesh M. Rajput 46


Casting

• We can specify the data type of a variable, this can be done


with casting.

x = str(3) # x will be '3'


y = int(3) # y will be 3
z = float(3) # z will be 3.0

Dr. Yogesh M. Rajput 47


Casting
x = int(1) # x will be 1
y = int(2.8) # y will be 2
z = int("3") # z will be 3

x = float(1) # x will be 1.0


y = float(2.8) # y will be 2.8
z = float("3") # z will be 3.0
w = float("4.2") # w will be 4.2

x = str("s1") # x will be 's1'


y = str(2) # y will be '2'
z = str(3.0) # z will be '3.0'

Dr. Yogesh M. Rajput 48


Task
• Write python program to create two
variable (country_rank, total_population)
to Store India's Country Rank and Total
Population.

• Convert these variable in float data type.

Dr. Yogesh M. Rajput 49


Operators

Dr. Yogesh M. Rajput 50


Operators
• Operators are used to perform operations on variables and
values.

• In the example below, we use the + operator to add together


two values:

print(25 + 2)

Dr. Yogesh M. Rajput 51


Python Operators
• Arithmetic operators

• Assignment operators

• Comparison operators

• Logical operators

• Identity operators

Dr. Yogesh M. Rajput 52


Arithmetic Operators
• Arithmetic operators are used with numeric values to perform common
mathematical operations:
Operator Name Example
+ Addition x+y
- Subtraction x-y
* Multiplication x*y
/ Division x/y
% Modulus x%y
** Exponentiation x ** y
// Floor division x // y

Dr. Yogesh M. Rajput 53


Arithmetic Operators

Dr. Yogesh M. Rajput 54


Arithmetic Operators

Dr. Yogesh M. Rajput 55


Assignment operators
• Assignment operators are used to assign values to variables:
Operator Example Same as
= x=5 x=5
+= x += 3 x=x+3
-= x -= 3 x=x-3
*= x *= 3 x=x*3
/= x /= 3 x=x/3
%= x %= 3 x=x%3
//= x //= 3 x = x // 3
**= x **= 3 x = x ** 3

Dr. Yogesh M. Rajput 56


Assignment operators

Dr. Yogesh M. Rajput 57


Task
• Write python program to create 7
variables and use assignment operators
to perform following operations.

• +=, -=, *=, /=, %=,**=,//=

Dr. Yogesh M. Rajput 58


Comparison Operators
• Comparison operators are used to compare two values:

Operator Name Example


== Equal x == y
!= Not equal x != y
> Greater than x>y
< Less than x<y
>= Greater than or equal to x >= y
<= Less than or equal to x <= y

Dr. Yogesh M. Rajput 59


Comparison Operators

Dr. Yogesh M. Rajput 60


Comparison Operators

Dr. Yogesh M. Rajput 61


Task
• Write a Python program that to store the
marks of two students, and use
comparison operators to display both
true and false conditions.

Dr. Yogesh M. Rajput 62


Logical Operators
• Arithmetic operators are used with numeric values to perform common
mathematical operations:

Operator Description Example


and Returns True if both statements are true x < 5 and x < 10
or Returns True if one of the statements is true x < 5 or x < 4
not Reverse the result, returns False if the result is true not(x < 5 and x < 10)

Dr. Yogesh M. Rajput 63


Logical Operators

Dr. Yogesh M. Rajput 64


Logical Operators

Dr. Yogesh M. Rajput 65


Logical Operators

Dr. Yogesh M. Rajput 66


Logical Operators

Dr. Yogesh M. Rajput 67


Logical Operators

Dr. Yogesh M. Rajput 68


Logical Operators

Dr. Yogesh M. Rajput 69


Logical Operators

Dr. Yogesh M. Rajput 70


User Input

Dr. Yogesh M. Rajput 71


User Input
• Python allows for user input.

• That means we are able to ask the user for input.

username = input("Enter username:")


print("Username is: " + username)

Dr. Yogesh M. Rajput 72


Thank You.

Dr. Yogesh M. Rajput 73

You might also like