To get the notes of python of MCA batchs

ankursenchetu 22 views 48 slides Oct 07, 2024
Slide 1
Slide 1 of 48
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

About This Presentation

This is notes for python in MCA batch.
H


Slide Content

25-09-2020 Side 1
MadanMohan MalaviyaUniv. of Technology, Gorakhpur
PROGRAMMING IN PYTHON
MCA-161A
4 Credits (3-0-2)
MCA 5
th
Sem (2020-21)
R K Dwivedi
Assistant Professor
Department of ITCA
MMMUT Gorakhpur

25-09-2020 Side 2
MadanMohan MalaviyaUniv. of Technology, Gorakhpur
Programming Basics and Decision Making
1.Introduction:
KeyfeaturesandapplicationsofPython,
PythonEditorsandCompilers(Interpreters),
UsingdifferentofflineandonlinePythonIDE,
InteractingwithPythonprograms
2.Datatypes:
Numeric,Boolean,Strings,Lists,Tuples,Sets,Dictionary
3.Variables:
Declarationandinitialization
4.SimpleStatements:
Takinginputsfromuser,Displayingoutputs
5.Otherconcepts:
Operators,Expressions,Indentation,Comments,Casting
6.Conditionalstatements:
if…elif…else
Contents

25-09-2020 Side 3
MadanMohan MalaviyaUniv. of Technology, Gorakhpur
1.Introduction

25-09-2020 Side 4
MadanMohan MalaviyaUniv. of Technology, Gorakhpur
1. Introduction
Keyfeatures
•Itismostwidelyused,high-level,general-purpose,multi-purposeandinterpretedprogramminglanguage.
•TherearetwomajorPythonversions:Python2andPython3(latestversionis3.7.1,wecansimplycallitasPython3).
•ItwascreatedbyGuidoVanRossum,andreleasedin1991.Python3.0wasreleasedin2008.
•ItisdevelopedunderanOSI-approvedopensourcelicense,makingitfreelyusableanddistributable,evenfor
commercialuse.
•Python works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc). It is portable across operating systems.
•Python has a simple syntax similar to the English language.
•Python runs on an interpretersystem, meaning that code can be executed as soon as it is written.
•Python can be treated in a proceduralway, an object-orientatedway or a functional way.
•Itsdesignphilosophyemphasizescodereadability.
•Itssyntaxallowsprogrammerstoexpressconceptsinfewerlinesofcode.
•Thebestwaywelearnanythingisbypracticeandexercisequestions.
•Python uses new linesto complete a commandinstead of semicolons or parentheses.
•Python uses indentationthrough whitespaceinstead of curly-brackets, to define the scope of loops, functions and classes.
•Python can connect to databasesystems. It can also read and modify files.
•It is very well suited for beginners and also for experienced programmers with other programming languages like
C++ and Java.
•The biggest strength of Python is huge collection of standard libraries that can be required inmany applications
(NumPy for numerical calculations, Pandas for data analytics etc).
•you can download it for free from the following website:https://www.python.org/

25-09-2020 Side 5
MadanMohan MalaviyaUniv. of Technology, Gorakhpur
1. Introduction
ApplicationsofPython
•Pythoncanbeusedonaservertocreatewebapplications.
•ItcanbeusedtocreateGUIbaseddesktopapplications(Games,ScientificandBusinessApplications).
•Itisalsousedtocreatetestframeworksandmultimediaapplications.
•Itisusedtodevelopoperatingsystemsandprogramminglanguage.
•It can be used to handle image processing, text processing and natural language processing.
•It can be used to create programs for machine learning, deep learning, data science, big dataand data analytics
applications.
•It can also perform complex mathematics along with all cutting edge technology in software industry.
Organizations and tech-giant companies using Python :
1) Google(Components of Google spider and Search Engine)
2) Yahoo(Maps)
3) YouTube
4) Mozilla
5) Dropbox
6) Microsoft
7) Cisco
8) Spotify
9) Quora
10) Instagram
11)Amazon
12)Facebook
13)Uber etc.

25-09-2020 Side 6
MadanMohan MalaviyaUniv. of Technology, Gorakhpur
1. Introduction
PythonEditorsandInterpreters
•Pythonisinterpretedlanguagei.eit’snotcompiledandtheinterpreterwill
checkthecodelinebyline.
•Codecanbewritteninatexteditorwith.pyextensionandthenitcanbeputintothe
pythoninterpreterforexecution.

25-09-2020 Side 7
MadanMohan MalaviyaUniv. of Technology, Gorakhpur
1. Introduction
UsingdifferentofflineandonlinePythonIDE
OfflineIDE: Thonny,Pycharm,Spyder,IDLE,NetbeansorEclipsePyDevetc.
OnlineIDE: https://www.w3schools.com/python/
https://www.w3resource.com/python/python-tutorial.php
https://www.geeksforgeeks.org/python-programming-language/
https://www.tutorialspoint.com/execute_python_online.php

25-09-2020 Side 8
MadanMohan MalaviyaUniv. of Technology, Gorakhpur
1. Introduction
InteractingwithPythonprograms

25-09-2020 Side 9
MadanMohan MalaviyaUniv. of Technology, Gorakhpur
2.Datatypes

25-09-2020 Side 10
MadanMohan MalaviyaUniv. of Technology, Gorakhpur
2. Data types
Pythonisdynamicallytypedlanguage(Noneedtomentiondatatypebasedonvalueassigned,ittakesdatatype)
Youcangetthedatatypeofanyobjectbyusingthetype()function

25-09-2020 Side 11
MadanMohan MalaviyaUniv. of Technology, Gorakhpur
2. Data types
NumericType:int,float,complex

25-09-2020 Side 12
MadanMohan MalaviyaUniv. of Technology, Gorakhpur
2. Data types
BooleanType:bool

25-09-2020 Side 13
MadanMohan MalaviyaUniv. of Technology, Gorakhpur
2. Data types
TextType(Strings):str
We can assign a string to a variable as below:
a ="Hello“
We can assign a multiline stringto a variable by using three single or three double quotes
as below:
(Note:in the result, the line breaks are inserted at the same position as in the code.):
a =’‘’Hi everyone,
This is Python Class.’‘’
a =”””Hi everyone,
This is Python Class.”””
To concatenate, or combine, two strings we can use the + operator as below:
a ="Hello"
b =“Class"
c = a +" "+ b
We cannot combine stringsand numberslike this:
age =30
txt ="My name is XYZ, I am "+ age

25-09-2020 Side 14
MadanMohan MalaviyaUniv. of Technology, Gorakhpur
2. Data types
TextType(Strings):str …continued

25-09-2020 Side 15
MadanMohan MalaviyaUniv. of Technology, Gorakhpur
2. Data types
TextType(Strings):str …continued

25-09-2020 Side 16
MadanMohan MalaviyaUniv. of Technology, Gorakhpur
2. Data types
SequenceType:list
Listisacollectionwhichisordered,indexedandchangeable.Itallowsduplicate
members.
Listsarewrittenwithsquarebrackets[].
Wecanaccesslistitemsbyreferringtotheindexnumber,insidesquare
brackets.

25-09-2020 Side 17
MadanMohan MalaviyaUniv. of Technology, Gorakhpur
2. Data types
SequenceType:list …continued

25-09-2020 Side 18
MadanMohan MalaviyaUniv. of Technology, Gorakhpur
2. Data types
SequenceType:tuple
Tupleisacollectionwhichisordered,indexedandunchangeable.Itallows
duplicatemembers.
Tuplesarewrittenwithroundbrackets().
Wecanaccesstupleitemsbyreferringtotheindexnumber,insidesquare
brackets.

25-09-2020 Side 19
MadanMohan MalaviyaUniv. of Technology, Gorakhpur
2. Data types
SequenceType:tuple …continued

25-09-2020 Side 20
MadanMohan MalaviyaUniv. of Technology, Gorakhpur
2. Data types
SetType:set
Setisacollectionwhichisunordered,unindexedandunchangeable.Itdoes
notallowduplicatemembers.
Setsarewrittenwithcurlybrackets{}.

25-09-2020 Side 21
MadanMohan MalaviyaUniv. of Technology, Gorakhpur
2. Data types
MappingType(Dictionary):dict
Dictionaryisacollectionwhichisunordered,indexedandchangeable.Itdoes
notallowduplicatemembers.
Dictionariesarewrittenwithcurlybrackets,andtheyhavekeysandvalues.
Wecanaccesstheitemsofadictionarybyreferringtoitskeyname,insidesquare
brackets.

25-09-2020 Side 22
MadanMohan MalaviyaUniv. of Technology, Gorakhpur
3.Variables

25-09-2020 Side 23
MadanMohan MalaviyaUniv. of Technology, Gorakhpur
3. Variables
Declarationandinitialization
Variablesarecontainersforstoringdatavalues.
Pythonhasnocommandfordeclaringavariable.
Avariableiscreatedthemomentyoufirstassignavaluetoit.
Rules for Python variables:
❖A variable name must start with a letter or the underscore character
❖A variable name cannot start with a number
❖A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9,
and _ )
❖Variable names are case-sensitive (age, Age and AGE are three different variables)
Variablesdonotneedtobedeclaredwithanyparticulartypeandcanevenchangetype
aftertheyhavebeenset.
Stringvariablescanbedeclaredeitherbyusingsingleordoublequotes.
Wecanassignvaluestomultiplevariablesinoneline.
Wecanassignthesamevaluetomultiplevariablesinoneline.

25-09-2020 Side 24
MadanMohan MalaviyaUniv. of Technology, Gorakhpur
3. Variables
Declarationandinitialization …continued

25-09-2020 Side 25
MadanMohan MalaviyaUniv. of Technology, Gorakhpur
4.SimpleStatements

25-09-2020 Side 26
MadanMohan MalaviyaUniv. of Technology, Gorakhpur
4. Simple Statements
Takinginputsfromuser
Python3.6usestheinput()methodwhilePython2.7usestheraw_input()method.

25-09-2020 Side 27
MadanMohan MalaviyaUniv. of Technology, Gorakhpur
4. Simple Statements
Displayingoutputs
printstatementisoftenusedtooutputvariables.
Toconcatenatetwoormorestringsorstringvariablesthe+characterisused.
Fornumbers,the+characterworksasamathematicaloperator.
Ifyoutrytocombineastringandanumber,Pythonwillgiveyouanerror.

25-09-2020 Side 28
MadanMohan MalaviyaUniv. of Technology, Gorakhpur
5.OtherConcepts

25-09-2020 Side 29
MadanMohan MalaviyaUniv. of Technology, Gorakhpur
5. Other Concepts
OperatorsandExpressions
•Arithmetic operators (+, -, *, /, %, **, //)
•Assignment operators (=, +=, -=, *=, /=, %=, **=, //=, &=, |=, <<=, >>=, ^=)
•Comparison operators (==, !=, <, >, <=, >=)
•Logical operators (and, or, not)
•Identity operators (is, is not)
•Membership operators (in, not in)
•Bitwise operators (&, |, ^, ~, <<, >>)

25-09-2020 Side 30
MadanMohan MalaviyaUniv. of Technology, Gorakhpur
5. Other Concepts
OperatorsandExpressions …continued
Arithmetic operators
** is used for Exponentiation and // is used for Floor division.

25-09-2020 Side 31
MadanMohan MalaviyaUniv. of Technology, Gorakhpur
5. Other Concepts
OperatorsandExpressions …continued
Logical operators

25-09-2020 Side 32
MadanMohan MalaviyaUniv. of Technology, Gorakhpur
5. Other Concepts
OperatorsandExpressions …continued
Identity operators

25-09-2020 Side 33
MadanMohan MalaviyaUniv. of Technology, Gorakhpur
5. Other Concepts
OperatorsandExpressions …continued
Identity operators

25-09-2020 Side 34
MadanMohan MalaviyaUniv. of Technology, Gorakhpur
5. Other Concepts
OperatorsandExpressions …continued
Membership operators

25-09-2020 Side 35
MadanMohan MalaviyaUniv. of Technology, Gorakhpur
5. Other Concepts
Indentation
Indentationreferstothespacesatthebeginningofacodeline.
Whereinotherprogramminglanguagestheindentationincodeisforreadabilityonly,
theindentationinPythonisveryimportant.
Pythonusesindentationtoindicateablockofcode.Otherlanguagesoftenusecurly-
bracketsforthispurpose.
Pythonwillgiveyouanerrorifyouskiptheindentation.
Thenumberofspacesisuptoyouasaprogrammer,butithastobeatleastone.
Youhavetousesamenumberofspacesinthesameblockofcode,otherwisePython
willgiveyouanerror.
CorrectExample:
if5>2:
print("Five is greater than two!")
if5>2:
print("Five is greater than two!")

25-09-2020 Side 36
MadanMohan MalaviyaUniv. of Technology, Gorakhpur
5. Other Concepts
Comments
Commentsstartswitha#,andPythonwillignorethem.
Pythondoesnotreallyhaveasyntaxformultilinecomments.
Toaddamultilinecommentyoucouldinserta#foreachline.
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.

25-09-2020 Side 37
MadanMohan MalaviyaUniv. of Technology, Gorakhpur
5. Other Concepts
Casting
Castingcanbedoneusingconstructors:int(),float(),str()

25-09-2020 Side 38
MadanMohan MalaviyaUniv. of Technology, Gorakhpur
6.ConditionalStatements

25-09-2020 Side 39
MadanMohan MalaviyaUniv. of Technology, Gorakhpur
6. Conditional Statements
if…elif…else
a =200
b =33
ifb > a:
print("b is greater than a")
elifa == b:
print("a and b are equal")
else:
print("a is greater than b")
If you have only one statement to execute, you can put it on the same line as the if
statement.
ifa > b:print("a is greater than b")

25-09-2020 Side 40
MadanMohan MalaviyaUniv. of Technology, Gorakhpur
6. Conditional Statements
if…elif…else

25-09-2020 Side 41
MadanMohan MalaviyaUniv. of Technology, Gorakhpur
6. Conditional Statements
if…elif…else
Ternary Operators, orConditional Expressions
If you have only one statement to execute, one for if, and one for else, you can put it
all on the same line.
a=2
b =330
print("A")ifa > belseprint("B")
You can also have multiple else statements on the same line.
a =330
b =330
print("A")ifa > belseprint("=")ifa == belseprint("B")
Logical operators to combine conditional statements
Theand/orkeywords are logical operators, and can be used as below:
ifa > b and a > c:
ifa > b or a > c:

25-09-2020 Side 42
MadanMohan MalaviyaUniv. of Technology, Gorakhpur
6. Conditional Statements
Ternaryoperator

25-09-2020 Side 43
MadanMohan MalaviyaUniv. of Technology, Gorakhpur
6. Conditional Statements
Logicaloperator

25-09-2020 Side 44
MadanMohan MalaviyaUniv. of Technology, Gorakhpur
6. Conditional Statements
if…elif…else
Nested if
You can have if statements inside if statements, this is called nested if
statements.
x =41
ifx >10:
print("Above ten,")
ifx >20:
print("and also above 20!")
else:
print("but not above 20.")

25-09-2020 Side 45
MadanMohan MalaviyaUniv. of Technology, Gorakhpur
6. Conditional Statements
Nestedif

25-09-2020 Side 46
MadanMohan MalaviyaUniv. of Technology, Gorakhpur
6. Conditional Statements
if…elif…else
Pass statement
if statements cannot be empty, but if you for some reason have an if statement
with no content, put in the pass statement to avoid getting an error.
a =33
b =200
ifb > a:
pass

25-09-2020 Side 47
MadanMohan MalaviyaUniv. of Technology, Gorakhpur
6. Conditional Statements
Passstatement

25-09-2020 Side 48
MadanMohan MalaviyaUniv. of Technology, Gorakhpur
Queries ?
Tags