An overview on python commands for solving the problems

Ravikiran708913 73 views 69 slides Jan 10, 2024
Slide 1
Slide 1 of 69
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43
Slide 44
44
Slide 45
45
Slide 46
46
Slide 47
47
Slide 48
48
Slide 49
49
Slide 50
50
Slide 51
51
Slide 52
52
Slide 53
53
Slide 54
54
Slide 55
55
Slide 56
56
Slide 57
57
Slide 58
58
Slide 59
59
Slide 60
60
Slide 61
61
Slide 62
62
Slide 63
63
Slide 64
64
Slide 65
65
Slide 66
66
Slide 67
67
Slide 68
68
Slide 69
69

About This Presentation

The complete overview on basic and simple commands on python, which will helps for all Science and engineering students.


Slide Content

“Lecture on Python”
by
Dr. RavikiranA. Mundewadi
Assistant Professor
Department of Mathematics
M.E.S College of Arts, Commerce and Science,
Malleswaram, Bengaluru-03

Guido van Rossum

•GuidoVanRossumisaDutchprogrammerbest
knownasthecreatorofthepythonprogramming
language.Whenhebeganimplementingpython,
GuidoVanRossumwasalsoreadingthe
publishedscriptsfrom“MonthlyPhythan’sFlying
Circus,aBBCcomedyseriesfromthe1970’s.
•VanRossumthoughtheneededanamethatwas
short,uniqueandslightlymysterious,sohe
decidedtocallthelanguagePhythan.

What is Python?
•Pythonisahigh-level,general-purposeprogramming
languagewithanelegantsyntaxthatallowsprogrammersto
focusmoreonproblem-solvingthanonsyntaxerrors.Oneof
theprimarygoalsofPythonDevelopersiskeepingitfunto
use.Pythonhasbecomeabigbuzzinthefieldof
modernsoftwaredevelopment,infrastructuremanagement,
andespeciallyinDataScienceandArtificialIntelligence.
Mostrecently,Pythonhasrisentothetop3listof
TIOBEindexoflanguagepopularity.
•Pythonisbecomingincreasinglyubiquitous,butyoumust
bewonderingwhyPythonhasbecomesuchahottopicinthe
developers’world.Inthistutorial,youwillunderstandall
reasonsbehindPython’spopularity.

•It is often used as a “Scripting language ” for
web applications. This means that it can
automate specific serious of tasks, making it
more efficient. Consequently python is often
used in software applications, pages within
web browser, the shells of operating systems
and same games.
What is python mainly used for?

Why is python so popular?
•More productive. First and foremost reason
why python is so much popular is so much
popular because it is highly productive as
compare to other programming language like
C++ and JAVA, etc. Python is also very famous
for its simple programming syntax, code
readability and English like commands that
make coding in python lot easier and efficient.

www.python.org

Download for Windows

Step 1: Download Python 3.9.1
•To start, go topython.org/downloadsand then click on the
button to download the latest version of Python:

Step 2: Run the .exe file
•Next, run the .exe file that you just
downloaded:

Step 3: Install Python 3.9.1
•You can now start the installation of Python by clicking
onInstall Now:

IDLE: Integrated Development and
Learning Environment

OUTPUT: Immediate mode
•>>> indicates ‘Prompt’

Open the New File

INPUT: Script mode

•This is how the syntax would look like in the
“untitled” box:

•PressF5on your keyboard. You will then get the
following message to save your code:
•Click on ‘OK’ to save the file
•Choose a location where the Python file will be
saved on your computer. You’ll also need to type
a name for your file. For example, type “Test” for
your file name:

Save the File Name
•It Save as “Test.py”
•“.py” indicates ‘Extension’
•Once you’re done, pressSave,and you’ll then see the “Hello
World” expression printed on your Python Shell:

Python Shell:
•It gives the output as “Hello world”

INSTALLING PACKAGES FOR PYTHON
•1. Go to search and type: cmdcommand
prompt or windows powershellis opened
•2. Type: python -m pip install --upgrade pip
•3. pip install numpy
•4. pip install sympy
•5. pip install matplotlib
•6. pip install scipy

Command Prompt

Windows PowerShell

Pip Installation

What is PIP?
•PIPisapackagemanagerforPythonpackages.
•Pipisoneofthemostfamousandwidelyused
packagemanagementsystemtoinstalland
managesoftwarepackageswritteninPython
andfoundinPythonPackageIndex(PyPI).Pip
isarecursiveacronymthatcanstandfor
either"PipInstallsPackages"or"PipInstalls
Python".Alternatively,pipstandsfor
"preferredinstallerprogram".

Fundamentals of Python

Statements
•Python statements are nothing but logical
instructions that interpreter can read and
execute. It can be both single and multiline.
There are two categories of statements in
Python:
•Expression Statements
•Assignment Statements

Expression Statement

Assignment Statements

Indentation

Comments

Variables

Constants

Tokens
•Tokens arebuilding blocksof a language. They
are the smallest individual unit of a program.
There are five types of tokens in Python.

Keywords
•Keywords:Keywordsarepredefinedwordswithspecial
meaningandthesearereservedwordsmustnotbeusedas
normalidentifiernames.ThesekeywordsconsistsofFalse,
True,None,and,or,not,as,is,in,if,elif,else,for,while,
assert,del,break,class,continue,def,except,global,finally,
from,import,lambda,nonlocal,pass,return,try,with,yield.

Identifiers
•Identifiers are the names given to variables,
objects, classes, functions, lists and so on.
•Variables names must not be key words and
must not contain blank spaces.
•Variable names are made up of (A-Z, a-z, 0-9,
underscore_) and must not start with a digit.
Valiedvariables names are ABCD, abcd, AbCd,
A345, a_bG78, _product.
•Invalid variable names are 6ab, avfd, for, and.

Literals

String Literals:
Astringliteralisasequenceofcharacterssurroundedbyquotes.We
canusebothsingle,doubleortriplequotesforastring.And,a
characterliteralisasinglecharactersurroundedbysingleordouble
quotes.
Numeric Literals:
NumericLiteralsareimmutable(unchangeable).Numericliterals
canbelongto3differentnumericaltypesInteger,Float,and
Complex.
Boolean Literals:
ABooleanliteralcanhaveanyofthetwovalues:TrueorFalse.
Collection literals:
TherearefourdifferentliteralcollectionsListliterals,Tupleliterals,
Dictliterals,andSetliterals.
Special literals:
Pythoncontainsonespecialliterali.e.None.Weuseittospecifyto
thatfieldthatisnotcreated.

Punctuators or Separators
•‘ indicates single quote
•“ indicates double quote
•# indicates for comment
•\indicates backslash
•( ) indicates parentheses
•[ ] indicates square brackets
•{ } indicates braces
•: indicates colon
•, indicates comma

Input and Output Statement
•Input statement : input() is used to input string, int(input()) is
used to input integer and float(input()) is used to input floating
value.
•Output statement:
•print statement :
•a=2
•b=3
•The print statement is a function its general form is
•Print(‘this is’,a,’thatis’,b)
•Where a and b are pre assigned variable names. If the above
statement is executed the output will be as , this is 2 that is 3,
anything written between two single quotes or between two
double quotes are considered as string.

Lists, Tupleand Functions
Lists: Lists are defined as a= [ 1,2,3,4], indexing start
with 0 i.e. a[0]=1,a[1]=2,a[2[=3, a[3]=4. The elements
of the list may be any data type and are mutable.
Tuple: Tuplesare the group of any data types separated
by commas and are enclosed in parenthesis.The
elements of tupleare immutable.
Example: a=(1,2,3), b=( ‘a’,2,”C”).
Functions : Functions are defined as
def name(arguments):
return expression
Example:
def f(x,y):
return x+y+2

Operators

Arithmetic OperatorsArithmetic Operators Operator Name Description Example
+ Addition Perform Addition
I=40, J=20
>>>I+ J
>>>60
- Subtraction Perform Subtraction
I=40, J=20
>>>I – J
>>>20
* Multiplication Perform Multiplication
I=40, J=20
>>>I * J
>>> 800
/ Division Perform Division
I=30, J=20
>>>I /J
>>> 2.5
% Modulus
Perform remainder after
Division
I=40, J=20
>>>I /J
>>> 0
** Exponent
Performs exponential
(power) calculation
I=4, J=20
>>>I /J
>>> 204
// Floor Division
Perform division
remove the decimal
value and return
Quotient value
I=30, J=20
>>>I//J
>>> 1

Assignment OperatorsOperator Operator Name Description Example
= Assignment It assigns value from
right side operand to
left side operand
I = 40
It assigns 40 to I
+= Add then assign It performs addition
and then result is
assigned to left hand
operand
I+=J
that means I = I + J
-= Subtract then assign It performs
subtraction and then
result is assigned to
left hand operand
I-=J
that means I = I – J
*= Multiply the assign It performs
multiplication and
then result is assigned
to left hand operand.
I*=J
that means I = I * J
/= Divide then assign It performs division
and then result is
assigned to left hand
operand
I/=J
that means I = I / J
%= Modulus then assign It performs modulus
and then result is
assigned to left hand
operand
I%=J
that means I = I % J
**= Exponent then assign It performs exponent
and then result is
assigned to left hand
operand
I**=J
that means I = I ** J
//= Floor division then
assign
It performs floor
division and then
result is assigned to
left hand operand
I//=J
that means I = I // J

Comparison operatorOperator Operator Name Description Example
== Equal to If the values of two
operands are equal,
then it returns true.
I = 20, J = 20
(I == J) is True
!= Not Equal to If the values of two
operands are not
equal, then it returns
true.
I = 20, J = 20
(I != J) is False
< Less than If the value of left
operand is less than
the value of right
operand, then it
returns true
I = 40, J = 20
(I < J) is False
> Greater than If the value of left
operand is greater
than the value of right
operand, then it
returns true
I= 40, J = 20
(I > J) is True
<= Less than or equal to If the value of left
operand is less than or
equal to the value of
right operand, then it
returns true
I = 40, J = 20
(I <= J) is False
>= Greater than or equal
to
If the value of left
operand is greater
than or equal to the
value of right
operand, then it
returns true.
I = 40, J = 20
(I >= J) is True
<> Not equal to (similar
to !=)
If values of two
operands are not
equal, then condition
becomes true
I=40, J = 20
(I <> J) is True.

•Comparison operator is also known as
Relational Operators because it compares the
values. After comparison, it returns the
Boolean value i.e. either true or false.

Bitwise Operators
•It performs bit by bit operation.
Suppose there are two variables,
I = 10 and
J = 20
and their binary values are:
I = 10 = 0000 1010
J = 20 = 0001 0100
now let us see how bitwise operators perform.

Operator Operator Name Description Example
& Binary AND If both bits are 1 then
1 otherwise 0
I & J
0000 0000
| Binary OR If one of the bit is 1
then 1 otherwise 0
I | J
0001 1110
^ Binary XOR If both bit are same,
then 0 otherwise 1
I ^ J
0001 1110
~ Binary Complement If bit is 1 the make it
0 and if bit is 0 the
make it 1
~I
1111 0101
<< Binary Left Shift The left operands are
moved left by the
number of bits
specified by the right
operand.
I << 2
240 i.e. 1111 0000
>> Binary Right Shift The left operands are
moved right by the
number of bits
specified by the right
operand.
I >> 2
15 i.e. 1111

Membership OperatorsOperator Description Example
in It returns true if it finds a
variable in the sequence
otherwise returns false
List = [1,2,3,4,5,6,7,8]
i=1
if i in List:
print(‘i is available in list’)
else:
print(‘i is not available in list’)
Output – i is available in list
not in It returns true if it does not
find a variable in the
sequence otherwise returns
false
List = [1,2,3,4,5,6,7,8]
j=10
if j not in List:
print (‘j is not available in list’)
else:
print (‘j is available in list’)
Output – j is not available in list

Identity Operators
•These operators are used to compare the
memory address of two objects.Operator Description Example
is It returns true if both operand ‘s
identity is same otherwise false
I = 20
J = 20
if(I is J):
print (‘I and J have same identity’)
else:
print (‘I and J have not same identity’)
Output – I and J have same identity
is not It returns true if both operand ‘s
identity is not same otherwise
false
I = 20
J = 230
if(I is not J):
print (‘I and J have not same identity’)
else:
print (‘I and J have same identity’)
Output – I and J have not same identity

Logical OperatorsOperator Operator Name Description Example
and Logical AND When Both side
condition is true the
result is true
otherwise false
2<1 and 2<3
False
or Logical OR When at least one
condition is true then
result is true
otherwise false
2<1 or 2<3
True
not Logical NOT Reverse the condition Not (5>4)
False

Python Conditional Statements
•if statement
•if else statement
•nested if statement
•for loop
•while loop

if statement

if else statement

nested if statement

for loop
The syntax of for loop is
for iin range(n):
S1
S2
S3
print()
First time iis initialized to zero and all the statements in the body of for loop(i.e.
S1,S2,S3) are executed once, then iincreased by 1, again all the statements in the
body are executed, again iincreased by 1 and the process continues ,when ireaches n
then the control comes out of for loop and the next statement (in this case it is the
print statement) is executed ( I takes the values from 0 to n-1).
for iin range(1,n):
S1
S2
S3
print()
In this case istart with 1and takes the values up to n-1.
for iin range(1,n,2):
S1
S2
S3
print()
In this case istart with 1 and takes the values less than n with increment 2.

while loop
The syntax of while loop is
while condition:
S1
S2
S3
print()
The while loop is executed as long as condition is true
if the condition is false then the control comes out of
the loop and the next statement is executed.

websites
•https://intellipaat.com/blog/tutorial/python-tutorial/
•https://www.codinground.com/tokens-python/
•https://datatofish.com/install-python/
•https://www.w3schools.com/python/default.asp
•https://realpython.com/
•https://www.programiz.com/python-programming/first-
program
•https://www.youtube.com/results?search_query=python
Tags