0% found this document useful (0 votes)
15 views48 pages

Introduction

The document provides a comprehensive overview of Python programming, covering topics such as language fundamentals, data types, functions, and libraries like Numpy and Pandas. It also discusses the differences between compiled and interpreted languages, the advantages of Python, and its history, including various versions. Additionally, it highlights the setup environment and the role of the Python Virtual Machine in executing Python code.

Uploaded by

Isha Shinde
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)
15 views48 pages

Introduction

The document provides a comprehensive overview of Python programming, covering topics such as language fundamentals, data types, functions, and libraries like Numpy and Pandas. It also discusses the differences between compiled and interpreted languages, the advantages of Python, and its history, including various versions. Additionally, it highlights the setup environment and the role of the Python Virtual Machine in executing Python code.

Uploaded by

Isha Shinde
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/ 48

Python

-
-
#
28 , 29 30 , 30 5 .
...
*

What are we going to cover ?


,

compiler , interpreters
language ,
indentifiers , procedures ,
types ,
function reference
collections
L
Fundamentals Functions Numpy

&
- -
--- -
-

pythor ,
variable , functions map/filter/reduce - ..
data
processing
Introduction
-
Functional Programming Pandas
- - -
- -

Compier +
interpreter ,
SDK ,
IDE is
object primorphism , .

- -
-
Setup OOP
- -
- -
- -
Data Analysis
numeris , list , set , dictionary ,
bool module
,
Book matplotlib ,
seaborn

Data Types Modules and Packages Data Visualization


-
- -

maths ,
relational ,
logical bitwise
,
closan
decorate
generator ,
,
metaclass selenium
Aop
Operators Advanced Features Testing
-
- -
-
- - -

control iterative ,
executes flask REST selenium ,
Beautiful Soup: Scrapy
, ,

Statements
- -
Web Services -
Web Scrapping
- -
About Instructor
§ 16+ years of experience
-

§ Associate Technical Director at Sunbeam


-
- --

§ Freelance Developer
-

§ Worked in various domains using different technologies


- - - - -

§ Developed 180+ mobile applications on iOS and Android platforms


---

§ Developed various websites using PHP, MEAN and MERN stacks


-
-

- -
--
- - -

§ Languages I love: C, C++, Python, JavaScript, TypeScript, PHP Go ,


Rust
-------

-
-

--
- -
- -

-
>
Language
Fundamentals
=
--
What is a computer language?

=
-

iron
§ A language is a medium to interact with computer
----
·

§ A language is used to solve a problem by giving some solution (writing a program)


- - - - - -

§ Types
§ Machine languages: the code written in 0s and 1s and can be directly executed by CPU
① -------
instruction
O
2 § Assembly languages : a language closely related to one or a family of machine languages, and which uses mnemonics
& - -

to ease writing
- - - - - - -

CDO family
·Is ARISC
SPARC
-
As
§ Programming languages
, ,

§ General purpose languages: a programming language that is broadly applicable across application domains, and lacks
Ct ,

Is
,

Pythonjava
,
-

specialized features for a particular domain


, - - - - -

CIF UANL
,
-

XML
MiralGrest
,
§ Markup languages: a grammar for annotating a document in a way that is syntactically distinguishable from the text
,

S
§ Stylesheet language: a computer language that expresses the presentation of structured documents
-
S ML
*§ Configuration language: a language used to write configuration files
umt
/

json
, - -

§ Query language: a language used to make queries in databases and information systems
-

sah <S
JS Ruby § Scripting languages : a language used to write simple to complex scripts
,

- -

puthonDeal
,
,

PH4 ,
Az
10000
--
+ 20000 -
Al
data ⑭

El

#oran
-

Assemblylanguage
e

storage
- -
e
collection
machine language
-

current
CPU-Electric
-
Low Level vs High Level Languages
- -

High Level Language Low Level Language

It is programmer friendly language It is a machine friendly language


High level language is less memory efficient Low level language is high memory efficient
It is easy to understand It is tough to understand
It is simple to debug It is complex to debug comparatively
It is simple to maintain It is complex to maintain comparatively
It is portable It is non-portable
It can run on any platform It is machine-dependent
It needs compiler or interpreter for translation It needs assembler for translation
E.g. Python E.g. Assembly
ildlande organiFi
· I

Father
- -
os independent
faster
-


file asm code
object
A
-
-
---

( .

obj)
↓ -
Libraries asm code

lit

definitions

#
Linker

#issue
. . .
pative
- -

.....
- -

& & -

Executable ...........

-
- -

OS dependent
-

magic number
Executable file format
--

① windows - > PE/coff


-
-

ELF
② Linuf
-
>
-
-

② macos I Mach-O
- -
-
Compiled language
--
--

§ A programming language which involves an executable to execute the logic instead of executing the
source code directly
§ E.g. C, C++, Pascal, Objective-C, Swift etc.
§ Stages
§ Pre-processing
§ Used to preprocess the code
§ Comments removal
§ Code expansion
§ Conditional compilation
§ Compiling
§ Compiler is used to translate pre-processed code into assembly instructions specific to the target processor architecture
§ Assembling
§ Assembler is used to translate the assembly instructions to object code
§ Linking
§ Linker used in this stage re-arranges the code and insert missing files to emit an executable
Interpreted Language
- -
--

§ A language mostly executes source code directly and freely, without previously compiling a programm
into an executable
§ E.g. Python, JavaScript, Perl, BASIC etc.
§ Interpretation
§ Interpreter used in the language executes the high level source code directly
Compiler vs Interpreter

Compiler Interpreter

The compiler is faster, as conversion occurs The interpreter runs slower as the execution
before the program executes. occurs simultaneously.
Errors are detected during the compilation Errors are identified and reported during the
phase and displayed before the execution of a given actual runtime.
program.
Compiled code needs to be recompiled to run Interpreted code is more portable as it can run
on different machines. on any machine with the appropriate
interpreter.
It requires more memory to translate the It requires less memory than compiled ones.
whole source code at once.
Debugging is more complex due to batch Debugging is easier due to the line-by-line
processing of the code. execution of a code.
Python
-
--
>
- console
Introduction
I
>
-

>
-
GUI
websites
*

§ Python is a general purpose, high level and interpreted language


- - - - -

§ It is dynamically typed and garbage collected language


-
- - -d
int
-
num = 100 ; = m
= 100

Python
-

§ It supports programming paradigms like


-
automatic memory
C

§ Procedural programming -
-
-
> scripting
Mar,agement

§ Object oriented programming


- - - -

§ Functional programming
-

§ Aspect oriented programming (metaprogramming and magic metaobjects)


- - - - -
-

§ Heavily dependent on indentation to create blocks which makes it very easy to read the code
----

§ It has more than 300,000 packages included with wide range of functionality
----
§ Graphical user interfaces
-

Skinter
-

§ Web frameworks
- - -
-
Flask/Dango
§ Networking
-
>
- Det/socket
§ Automation >
- Selenium
selenium/Bs) Scrapy
-

§ Web scraping
- - -
~
History
-

§ Python was conceived in the late 1980s by Guido Rossum in Netherlands


-----

§ It is considered as a successor to the ABC language (itself inspired by SETL)


------

§ Its implementation began in December 1989


---

§ Van Rossum continued as Python's lead developer until July 12, 2018
------

§ When he announced his permanent vacation from his responsibilities, a team of five members was
developed in Jan 2019 to lead the project
- - - - - -

--- >
-
Advantages of Python
-

§ Python is simple to use, but it is a real programming language, offering much more structure and
support for large programs than shell scripts or batch files can offer
§ Python also offers much more error checking than C, and, being a very-high-level language, it has high-
level data types built in, such as flexible arrays and dictionaries
§ Because of its more general data types Python is applicable to a much larger problem domain than Awk
or even Perl, yet many things are at least as easy in Python as in those languages
§ Python allows you to split your program into modules that can be reused in other Python programs
§ Python is an interpreted language, which can save you considerable time during program development
because no compilation and linking is necessary
§ Python enables programs to be written compactly and readably
§ Programs written in Python are typically much shorter than equivalent C, C++, or Java programs, for
several reasons:
§ the high-level data types allow you to express complex operations in a single statement
§ statement grouping is done by indentation instead of beginning and ending brackets
§ no variable or argument declarations are necessary
Differences between Python and C Language
C Language Python

C is procedure-oriented programming language. It does Python is object-oriented oriented language. It contains


not contain the features like classes, objects, inheritance, features like classes, objects, inheritance, polymorphism,
polymorphism, etc. etc.
Pointers concept is available in C Python does not use pointers
C has switch statement Python does not have switch statement
C programs execute faster Python programs are slower compared to C. PyPy flavor
or Python programs run a bit faster but still slower than
C
Compulsory to declare the datatypes of variables, arrays Type declaration is not required in Python
etc
C does not have exception handling facility and hence C Python handles exceptions and hence Python programs
programs are weak are robust
C does not contain a garbage collector Automatic garbage collector is available in Python
C supports in-line assignments Python does not support in-line assignments
Indentation of statements is not necessary in C Indentation is required to represent a block of
statements
Versions
§ Version 0.9.0 [Feb 1991]
--
--
-

§ Having features like classes with inheritance, exception handling, functions etc.
§ One of the major versions of python
§ Version 1 [Jan 1994]
- -

§ The major new features included in this release were the functional programming tools like labda, map, filter, reduce
§ The last version released was 1.6 in 2000
§ Version 2 [Oct 2000]
>
-
-
- -

§ Introduced features like list comprehension, garbage collection, generators etc.


§ Introduced its own license known as Python Software Foundation License (PSF)
§ The last version released was 2.7.16 in Mar 2019
§ Version 3 [Dec 2008]
°
- -

§ Python 3.0 is also called "Python 3000" or "Py3K”


§ It was designed to rectify fundamental design flaws in the language
§ Python 3.0 had an emphasis on removing duplicative constructs and modules
§ The last version released was 3.7.4 in Jul 2019
Environment Setup

=
§ Ubuntu
§ Install python3 on using apt installer
§ > sudo apt-get install python3 python3-pip

§ Download pycharm community edition


§ https://www.jetbrains.com/pycharm/download
os independent os dependent

#asFilm
bello PY
.

r For e makin
(
parsing Python Code

(syntax checking ete)


(DUN)
↓ -

code
bytecode (native executable)
preprocessing code
to asm

byte code assembles


What have you downloaded
§ Python has many versions like

Go
§ CPython #t e fastest

§ Pypy
§ Jython
§ InronPython
§ The standard version is called as CPython which is what most of us get when we download from the
-----

official site
-

§ Henceforth when we say we are using python, it will be CPython


-
----
Hello world program explained
§ Python is not a compiled language: misconception
§ CPython has a compile base
§ Python compiles into bytecodes
§ CPU can not understand bytecodes
§ We need an interpreter to understand and execute the bytecodes
§ This interpreter is nothing but the python virtual machine
Python Virtual Machine (PVM)
§ Python compiler compiles the python source code into byte codes
§ Byte codes represent fixed set of instructions to perform all types of operations
§ The size of each byte code is 1 byte or 8 bits and hence the these are called as byte codes
§ The role of PVM is to convert these byte codes into machine code so that the computer can execute
those machine code instructions and display the final output
§ To carry out this conversion, PVM is equipped with an interpreter
§ The interpreter converts the byte code into machine code and sends that machine code to the computer
processor for execution
§ Since interpreter is playing the main role, often the Python Virtual Machine is also called an interpreter
Foundation
-
-
Identifier and keywords
-

-- num = 200
- - -
-

-
a

identifier
§ Identifiers
-
--

§ Identifiers or symbols are the names you supply for variables, types, functions, and classes
- - - - - - -

§ Identifier names must differ in spelling and case from any keywords
- - -

§ Keywords
-
-

§ Keywords are the identifiers which are reserved

F
=
--

§ They can not be used to naming variables, types, functions or classes


-
§ There are 35T
- ↑
- -
-
rue
keywords in python 3.8
bool False
①§ Value Keywords: True, False, None
-
- --
-

- . -
None >
-> null -
nothing if 7, 18 :
②§ Operator Keywords: and, or, not, in, is logical operators and or not
E
age
-
---- - = , ,
- 4
-

③§ Control Flow Keywords: if, elif, else


-

- - - - -
-
else :
④§ Iteration Keywords: for, while, break, continue, else
- - - - - - - - - -
-
->
-
--
-

⑤§ Structure Keywords: def, class, with, as, pass, lambda


-
-
·
- - - - - - - -

⑥§ Returning Keywords: return, yield (generator


----

⑰§ Import Keywords: import, from, as


- - - -

S §
O Exception-Handling Keywords: try, except, raise, finally, else, assert
------ -
-

④§ Asynchronous Programming Keywords: async, await


- - - -

⑭§ Variable Handling Keywords: del, global, nonlocal


----
Rules and Conventions
-m

§ Constant and variable names should have a combination of letters in lowercase (a to z) or uppercase (A
-------

to Z) or digits (0 to 9) or an underscore (_) "amit"


& first
-- -
DAM =
/
n

§ If you want to create a variable name having two words, use underscore to separate them
-
-

6
-- space
§ Create a name that makes sense (use meaningful names) name :
"Amit"
-- -
pychone
§ Use capital letters possible to declare a constant Pl 3 14 =
.

Java >
- firstName
-- -
-
-

§ Never use special symbols like !, @, #, $, %, etc.


- - - - - -
cust (camel)

§ Don't start a variable name with a digit For comments


# used
- - -

§ Identifiers are case sensitive


- -
game ↑ -

a
name
Name are different
&i st-name or namel
Python is dynamically-typed language
Variable
- -
-

§ A variable is a named location used to store data in the memory


--

§ It is helpful to think of variables as a container that holds data that can be changed later
-----

§ E.g. - python all data types


are

§ value = 20 >
-
assigned) variable declaration 0x27
=
inferred Canto IEl
§ name = "steve"
-
--
- -

Eithere
Dum
--
num =
-
-
-
-
-

(iet) * int (2 buses

Salary Hoat 32

I byte : 8 bits
one
max =
28 : 256 - -

Valle =
0-255
-
-
logical address

#Fire
address
-

·t
-
on
↓ (RAND)
- -

primary mem

Start int
>
-
a um = 200
tum right Sh
RUM I "test"


Constants
--
-

§ Constants are written in all capital letters and underscores separating the words
--- - -

§ E.g.
§ PI = 3.14
-
- -

§ In reality, we don't use constants in Python


- -

§ Naming them in all capital letters is a convention to separate them from variables, however, it does not
-----

actually prevent reassignment


--
Statements num = 200
CDUR

- - invalid
statement
§ Instructions that a Python interpreter can execute are called statements Cassignment)
a(n)
- - - - - -

§ E.g., a = 1 is an assignment statement

i
- - -
- St
I 20

; Line - NUM
§ Single line statements
/S / -

- optional Line 2 - print (numi


e Ste

§ The end of a statement is marked by a newline character


----
-
new
-
like

§ Multi-line statement
--

§ We can make a statement extend over multiple lines with the line continuation character (\)
A
- - - -
§ Line continuation is implied inside parentheses ( ), brackets [ ], and braces { }
int(num)
------
:
§ We can also put multiple statements in a single line using semicolons ** line t = 200
;
- -
S+ 2
Stl ↑
§ Comments
-
mandatory
§ Comments are very important while writing a program
-

§ They describe what is going on inside a program, so


- - -
-
that a person looking at the source
-
code does not have a hard time
figuring it out
-

§ In Python, we use the hash (#) symbol to start writing a comment


--
⑳ I

data

Data Types
-
-
Need of Data Types

=
§ Data type is an attribute associated with a piece of data that tells a computer system how to interpret
its value
§ Understanding data types ensures that data is collected in the preferred format and the value of each
property is as expected
§ It helps to know what kind of operations are often performed on a variable

-
-
Ster dynamically
statically tuped
typed - Python ,

C c ++ JS
>
-
,
Python Data Types

Data Types

/ ordered
collection key-value True False
q pairs
-collection
quales
Numeric Dictionary
- -
Boolean -
Sequence Set

numberoes
person : & can-vote : True unordered &
collection
mutable unique
: False q
u
"name"
-
: "PI'
- - is-valid
Integer 3

ahe -
List
Key
immutable

Float Tuple
- -

collection
Comp = 2015 1 qcharacters
Complex -
String
-

+
real
imaginally
Literals 200
A
num =
-
--

°
-

> -
-

§ Literal is a raw data given in a variable or constant


-

--- variable literal


- -
-

num : 300
§ Numeric Literals
- -

§ Numeric Literals are immutable (unchangeable)


- - - -

§ Numeric literals can belong to 3 different numerical types:


- - - -

§ Integer (value = 100)


- -

§ Float (salary = 15.50) quates


quotes
- >
-
- -

§ Complex (x = 10 + 5j) single


-
-
- -

-
double
-

1
§ String literals
- --
- -
-
-
single line
§ A string literal is a sequence of characters surrounded by quotes -
> tilinea
- -

§ We can use both single, double, or triple quotes for a string


- -

§ E.g., name = “steve” or name = ‘steve’ quotes


-
Literals
§ Boolean literals

Kersors
ie
-

§ A Boolean literal can have any of the two values: True or False
- - - - -

§ E.g., can_vote = True or can_vote = False


- -
- n

§ Special literal
--

§ Python contains one special literal i.e. None


- - >
-
- -
one
§ We use it to specify that the field has not been created
X
- -

§ E.g., value = None


-
Duri
-

§ Literal Collections
- -

§ Used to declare variables of collection types


§ There are four different literal collections List literals, Tuple literals, Dict literals, and Set literals
List = []
tuple = ci
set =
43
dictionary =
<3
Type Hinting

§ Due to the dynamic nature of Python, inferring or checking the type of an object being used is especially
hard
§ This fact makes it hard for developers to understand what exactly is going on in code they haven't
written. As a result they resort to trying to infer the type with around 50% success rate.
§ Why type hints?
§ Helps type checkers: By hinting at what type you want the object to be the type checker can easily detect if, for
instance, you're passing an object with a type that isn't expected
§ Helps with documentation: A third person viewing your code will know what is expected where, ergo, how to use it
without getting them TypeErrors
§ Helps IDEs develop more accurate and robust tools: Development Environments will be better suited at suggesting
appropriate methods when know what type your object is
Type Conversion
§ The process of converting the value of one data type (integer, string, float, etc.) to another data type is
called type conversion
§ Python has two types of type conversion.
§ Implicit Type Conversion
§ Python automatically converts one data type to another data type
§ This process doesn't need any user involvement
§ E.g.
§ result = 10 + 35.50
§ Explicit Type Conversion
§ Users convert the data type of an object to required data type
§ We use the predefined functions like int(), float(), str(), etc to perform explicit type conversion
§ E.g.
§ num_str = str(1024)
Type Conversion
§ Type Conversion is the conversion of object from one data type to another data type
§ Implicit Type Conversion is automatically performed by the Python interpreter
§ Python avoids the loss of data in Implicit Type Conversion
§ Explicit Type Conversion is also called Type Casting, the data types of objects are converted using
predefined functions by the user
§ In Type Casting, loss of data may occur as we enforce the object to a specific data type
Operators
Operators
§ Operators are special symbols in Python that carry out arithmetic or logical computation
§ The value that the operator operates on is called the operand
§ Types
§ Arithmetic operators
§ Comparison operators
§ Logical operators
§ Bitwise operators
§ Special operators
§ Membership operators
Arithmetic Operators

+ Add two operands or unary plus x+y+2

- Subtract right operand from the left or unary minus x–y

* Multiply two operands x*y

/ Divide left operand by the right one (always results into float) x/y

% Modulus - remainder of the division of left operand by the right x%y

Floor division - division that results into whole number adjusted to


// x // y
the left in the number line

** Exponent - left operand raised to the power of right x**y


Arithmetic Operators
§ Precedence
§ Decides how the expression will be solved
§ Brackets ()
§ Power Of (**)
§ Multiplication (*)
§ True Division (/)
§ Floor Division (//)
§ Addition (+)
§ Subtraction (-)
§ Associativity
§ When there are two ore more operators in an expression with same precedence then associativity is used to decide the
way to solve the expression
§ All the operators have left to right associativity except power operator (which is right to left)
Comparison operators

> Greater than - True if left operand is greater than the right x>y

< Less than - True if left operand is less than the right x<y

== Equal to - True if both operands are equal x == y

!= Not equal to - True if operands are not equal x != y

Greater than or equal to - True if left operand is greater than or


>= x >= y
equal to the right

Less than or equal to - True if left operand is less than or equal to


<= x <= y
the right
Logical operators

and True if both the operands are true x and y

or True if either of the operands is true x or y

not True if operand is false (complements the operand) not x


Assignment Operators

= x=5 x=5 **= x **= 5 x = x ** 5

+= x += 5 x=x+5 &= x &= 5 x=x&5

-= x -= 5 x=x-5 |= x |= 5 x=x|5

*= x *= 5 x=x*5 ^= x ^= 5 x=x^5

/= x /= 5 x=x/5 >>= x >>= 5 x = x >> 5

%= x %= 5 x=x%5 <<= x <<= 5 x = x << 5

//= x //= 5 x = x // 5
Bitwise operators

& Bitwise AND x & y = 0 (0000 0000)

| Bitwise OR x | y = 14 (0000 1110)

~ Bitwise NOT ~x = -11 (1111 0101)

^ Bitwise XOR x ^ y = 14 (0000 1110)

>> Bitwise right shift x >> 2 = 2 (0000 0010)

<< Bitwise left shift x << 2 = 40 (0010 1000)


Special operators

is True if the operands are identical (refer to the same object) x is True

True if the operands are not identical (do not refer to the same x is not
is not
object) True
Membership Operators

in True if value/variable is found in the sequence 5 in x

not in True if value/variable is not found in the sequence 5 not in x

You might also like