0% found this document useful (0 votes)
22 views

Python

1. Python is a programming language that allows users to write step-by-step instructions to perform tasks. 2. The workflow of a Python program involves writing source code, passing it through a syntax checker, compiler, and interpreter before being run on a Python engine. 3. To use Python, a programming environment like IDLE needs to be installed which provides tools like a shell and document editor to write and run Python code.

Uploaded by

Michael Devasia
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Python

1. Python is a programming language that allows users to write step-by-step instructions to perform tasks. 2. The workflow of a Python program involves writing source code, passing it through a syntax checker, compiler, and interpreter before being run on a Python engine. 3. To use Python, a programming environment like IDLE needs to be installed which provides tools like a shell and document editor to write and run Python code.

Uploaded by

Michael Devasia
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Python

Python Programming Language It is a programming


which write step by step
language by using we

instructions to machine to perform the tasks

PL
If
Translator LLL Machine

Converts Machine
ALL code
HLLE LLL
source CMLL
code Machine
t level
code written language
human O's 41 s
by

Translator

Compiler Interpreter
will take the entire code will take one line at a time
D
and convert to Licata and convert the code to
time LLL

2 Hard to debug Easy to debug

For us to use the Python programming language


we need to use it in the environment

so first this environment needs to be installed


Things to learn

D Syntax Runes we have to follow these rules


to write the Python programming language
2 Code

WorkflowofPgth nStreator
syntax compiler
checker Babe

v
exceptions
Machine Interpreter
My C

source code python virtual


SCC Machine

Python Engine

1 Source code is written


2 The Code will be passed to the python Engine
3 On Python engine the code first goes through the

suntan checker to check syntax


there is error we will get
At this stage if an

a syntax error

the syntax check the code will be given


4 After
to compiler if no syntax error exists

5 Compiler converts the source code to the byte


code called intermediate code

G since byte code cannot be understood by


machines we have a Python virtual machine

7 Python virtual machine has a interpreter in it

which converts from byte code to machine code


and is passed to a machine

Errors created when converting byte code to


code are called Exceptions run
machine
firmed
in Python Here Error is logic error

we use both compiler and interpreter here to


make it little faster Also it would give out

logical errors

of finding out
Debugging is the process the error

and rectifying it
Python Environment
two Python 2 version
Versions Python 3 version

when we install PE Python Environment we set a

tool called IDLE Integrated Development and


learning Environment i e team and apply
two shell
tools Document Editor

we can use Python programming language in these


tools

shed tool where we can use python programming


language
Shell can only take single line code at a time

multiple code lines at a time we cannot


For
use shell
Code once executed cannot be saved
Single line input and output query
Shell Pista house
ex
Input
output I
Input
output
I I
0 o q
I Earn
blinking
lineate
customer
of your
9a chef
Python
Engine
Rent Editor
1 simillar to shell but here we can execute multiple
lines of code at a time

2 Document editor code can be saved for future


execution
3 Python code when it is saved it will be saved with

an extention of ops i e file Py


4 Document editor has use interface problem because
input is in one window and output is in another
window

so instead of shell or Document Editor we use

Jupiter notebook
other tools that we can use are

visual studio
Spyder
Py charm
Atom

To install Jupiter notebook we install an application


called Anaconda with this we get
Jupiter notebook
visual studio

Spyder
Jupiter notebook will save the file as ipynb
ipyn b i python notebook

while using Jupiter notebook the command prompt


should not be closed
R is best for statistics purpose but
Language
cannot be used for deep learning
Different cases in Naming convention

In spaces are reserved characters


programming
so if the name of the variable is more than
just one word we cannot use spaces to separate
them
number of digits 34 X
here each word is treated as separate entity as

space is in between them

For our
program to work correctly we need to

remove all spaces and combine the words into

a single string in a specific way And there are

a few ways available to combine them

Snake case Snake case separates each word


with an underscore character

ex Enlai
number of digits 34

snake case is used for variables and identifiers

Kebab case simillar to snake case but uses

dash C character instead oooo


kebab
ex
number of digits 34

mostly used in URL's


Camel case when using camel case we start

by making the first word lower case Then


we capitalize the first letter of each word
that follows
ex number Of Digits 34 farmer
used for function names in Python

is similar to camel
Pascal case pascal case

case The only difference is in pascal case even

the first letter of the first word is also

capital case

ex

Number Of Digits 34
used for class names in python

we use different cases for different objects to


easily differentiate them
Python Syntax
Statement It is a line or piece of code that python
can execute

Rutet single line statement


statement should be written in a single line
single
ex It I
2 2

It I 2 2 X

2 Multi line statement single statement in


multiple lines

whenever we want to write single statement in

multiple lines we have to use backward slash i

exe I 1 It 1 25
It 1 It 3
z Z X U

python statement ends with a token in


New line character But we can extend the
statement over multiple lines using line
Continuation character l This is known
as explicit continuation

At the end we cannot use backslash l


we can write multi line statements inside
brackets C CJ 3 whatever we add
inside the brackets will be treated as a single
stament even if it is placed on multiple
lines This is called implicit continuation

Rule 3 Mutiple statements in a single line

want to write multiple statements


whenever we

in a single line we have to use semi colon

between the statements


ext it l 2 2

I I 2 2 X

output is only shown for the last statement

Ruley comments

These are the statements that the python Engine


ignores
whenever we want to convert a statement in to

comment we use hash before the statement

It I

In Python we will have only single line comments

It I
It 2 3 4 I
On Python multi line comments are not supported
triple quote concept does not work

Ruleta Indentation
Indentation is used to specify a block of code
Default Indentation is four white spaces
ex if true

Print hi
we white
four spaces
we can also use one two or three white spaces

If no white space is used we will get syntax


error

Rule 6 keywords
keywords are the reserved words which will
have a special meaning in Python Engine PE

In Python Version 3 we have around 36 keywords


ex if for and else
out of 36 keywords there will be 3 keywords
with first word capital
ext True False None

All other keywords i e around 33 will have all


lower case

Ralf Identifies
Identifier is a name given to an entity i.e a class
Ide tf g Ey c e

a variable function module etc

Entity is a single occurrence of something objects


ext Objects to is given a name and this name

is an identifier

Rule Variable
simplified definition It is a container where we will

store data
variable is a subset of identifier
ext 1 pm

q Lunch
L Office
food data
Tiffin box container variable
To Identify Yan able Identifier we have to
name them

Naming convention for variable Identifier


These are set rules by which we will try to name

the variable or Identifier


1 The variable name should start with an alphabet
upper or lower case or underscore G
2 Name should not start with a number o g or

special character E we spaced


3 Variable names can be a mixture of alphabets or

numbers A Z A Z or 0 9

4 Variable names should not use any special


C E char ctersv

5 It is a case sensitive language


ex X and K are different variables

ex Albo
a be x
a 23
a 23

923 X
If naming convention is not used we will get a

syntax error

In Python data is assigned to a variable using


an Assignment operator
variable Data
Python programming language is case sensitive
we can
Assign asingle value to multiple variables
at the same time
ex a b c 10

we can Assign multiple values to multiple variables

at the same time

ext a b c 10 20,30

Variable scope The scope of the yan able refers


two to the places access a variable
types Local variable
Global Variable

Local variable A local variable is a variable

that is accessible inside a block of code only


where it is declared That means if we declare
a variable inside a function method the scope

of the local variable is limited to the method only

Global variable A Global variable is a variable

that is defined outside of function method


block of Code That is accessible anywhere
in the code file
Data It is a piece of information
There will be different Data types in Python

Python Data types

parfumeric Datatype
different string Datatype
types Boolean Data type
Collection Datatype

Numeric Data types


Integer
float
Complex

Integer Real numbers range is to to


ex 1 2 1 2 0

have decimal value


float we a 0 too

ext l l 2 1.5 10.5


floating point values can be represented
using the exponential form also called scientific
notation The benefit is we can represent large
values using less memory

ex 1.2224 i e 12200 0
Complex we have parts real and imaginary
at bi
ext 2 5

real imaginary
part part

imaginary part can only be J or J on Python

j should always be accompanied by real number

ex 2 3
2 1

The real integer value can be in the form of


either decimal float binary or hexadecimal
But the imaginary part should be represented
using the decimal form only we will get error

if we try binary hen or octal s


Function It is a name given to a piece of code

name of code function

Zac
when we want to execute this piece of pre
existing code we call this function
Functions
two Inbuilt function
types
user defined Junction

ÉfÉÉn Ine pre existing in Python programming


language
1 type C function
here C that function KC
represents calling ext

type c function is an Inbuilt function mainly used


to know the data type of the data

ex N 100

typeck
int integer

4 1 2
type Cy
complex
Type conversion It is a process by using which we

convert from one data type to another data


can
type
Type conversion
two Implicit conversion
types
explicit conversion

Implicit type conversion

This is the conversion of one data type in to another

data type by python itself during the execution


of the program
ex 2 o it 2

3 25 everything converted to complex

During implicit type conversion

first preference s complex


second preference float
third preference Integer
In implicit conversion we will not loose any data

ex It 2.2 3.2

Int Float Float

Explicit type conversion

when the user explicitly does the type conversion


For using explicit type conversion we take the
help of Constructors
Constructors a

simple definition is it will construct something


There are four types of Constructors
Integer constructor

float constructor
complex constructor
constructor will be taught later
string

Integer Constructor This will construct integer from


integer and float

inte
ex 2 10 4 10 z

intend intcy
o 10 we can loose data

float constructor this will construct float from


float and integer
floats
ex n 9 20.53

floater
Y float y
20 53

Complex constructor this will construct complex type

from integer float and complex

Complex C
ex 2 100 9 100 53
complex x Complex y
100 0
10040g
we cats loose data

In explicit conversion we can loose data

Type error this is an error when we are using


a type conversion
ex N It 2
intent
Type error as we cannot convert

complex to integer
Variable
Actual definition It is a name given to a

memory location
2 10

each Cell will have id


RAM
id una 10234

So here meal 0234 will be referred as n here


So variable is a temporary name given to the

memory location
Inbuilt function
id C this function gives the id of the memory
location
ex 2 10

iden
an al 0234

Python will not waste memory

for ene 2 10
y 10

So here y is not stored in separate space Instead


it will refer to se

variables data will have


So different having same

only one storage location

Fat
each Cell will have id
RAM
id Rna 10234

so iden is same as idly

You might also like