SlideShare a Scribd company logo
25-09-2020 Side 1
Madan Mohan Malaviya Univ. of Technology, Gorakhpur
PROGRAMMING IN PYTHON
MCA-161A
4 Credits (3-0-2)
MCA 5th Sem (2020-21)
R K Dwivedi
Assistant Professor
Department of ITCA
MMMUT Gorakhpur
25-09-2020 Side 2
Madan Mohan Malaviya Univ. of Technology, Gorakhpur
Programming Basics and Decision Making
1. Introduction:
Key features and applications of Python,
Python Editors and Compilers (Interpreters),
Using different offline and online Python IDE,
Interacting with Python programs
2. Data types:
Numeric, Boolean, Strings, Lists, Tuples, Sets, Dictionary
3. Variables:
Declaration and initialization
4. Simple Statements:
Taking inputs from user, Displaying outputs
5. Other concepts:
Operators, Expressions, Indentation, Comments, Casting
6. Conditional statements:
if…elif…else
Contents
25-09-2020 Side 3
Madan Mohan Malaviya Univ. of Technology, Gorakhpur
1. Introduction
25-09-2020 Side 4
Madan Mohan Malaviya Univ. of Technology, Gorakhpur
1. Introduction
Key features
• It is most widely used, high-level, general-purpose, multi-purpose and interpreted programming language.
• There are two major Python versions: Python 2 and Python 3 (latest version is 3.7.1, we can simply call it as Python 3).
• It was created by Guido Van Rossum, and released in 1991. Python 3.0 was released in 2008.
• It is developed under an OSI-approved open source license, making it freely usable and distributable, even for
commercial use.
• 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 interpreter system, meaning that code can be executed as soon as it is written.
• Python can be treated in a procedural way, an object-orientated way or a functional way.
• Its design philosophy emphasizes code readability.
• Its syntax allows programmers to express concepts in fewer lines of code.
• The best way we learn anything is by practice and exercise questions.
• Python uses new lines to complete a command instead of semicolons or parentheses.
• Python uses indentation through whitespace instead of curly-brackets, to define the scope of loops, functions and classes.
• Python can connect to database systems. 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 in many 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
Madan Mohan Malaviya Univ. of Technology, Gorakhpur
1. Introduction
Applications of Python
• Python can be used on a server to create web applications.
• It can be used to create GUI based desktop applications(Games, Scientific and Business Applications).
• It is also used to create test frameworks and multimedia applications.
• It is used to develop operating systems and programming language.
• 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 data and 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
Madan Mohan Malaviya Univ. of Technology, Gorakhpur
1. Introduction
Python Editors and Interpreters
• Python is interpreted language i.e it’s not compiled and the interpreter will
check the code line by line.
• Code can be written in a text editor with .py extension and then it can be put into the
python interpreter for execution.
25-09-2020 Side 7
Madan Mohan Malaviya Univ. of Technology, Gorakhpur
1. Introduction
Using different offline and online Python IDE
Offline IDE: Thonny, Pycharm, Spyder, IDLE, Netbeans or Eclipse PyDev etc.
Online IDE: 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
Madan Mohan Malaviya Univ. of Technology, Gorakhpur
1. Introduction
Interacting with Python programs
25-09-2020 Side 9
Madan Mohan Malaviya Univ. of Technology, Gorakhpur
2. Datatypes
25-09-2020 Side 10
Madan Mohan Malaviya Univ. of Technology, Gorakhpur
2. Data types
Python is dynamically typed language(No need to mention data type based on value assigned, it takes data type)
You can get the data type of any object by using the type( ) function
25-09-2020 Side 11
Madan Mohan Malaviya Univ. of Technology, Gorakhpur
2. Data types
Numeric Type: int, float, complex
25-09-2020 Side 12
Madan Mohan Malaviya Univ. of Technology, Gorakhpur
2. Data types
Boolean Type: bool
25-09-2020 Side 13
Madan Mohan Malaviya Univ. of Technology, Gorakhpur
2. Data types
Text Type (Strings): str
We can assign a string to a variable as below:
a = "Hello“
We can assign a multiline string to 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 strings and numbers like this:
age = 30
txt = "My name is XYZ, I am " + age
25-09-2020 Side 14
Madan Mohan Malaviya Univ. of Technology, Gorakhpur
2. Data types
Text Type (Strings): str …continued
25-09-2020 Side 15
Madan Mohan Malaviya Univ. of Technology, Gorakhpur
2. Data types
Text Type (Strings): str …continued
25-09-2020 Side 16
Madan Mohan Malaviya Univ. of Technology, Gorakhpur
2. Data types
Sequence Type: list
List is a collection which is ordered, indexed and changeable. It allows duplicate
members.
Lists are written with square brackets [ ].
We can access list items by referring to the index number, inside square
brackets.
25-09-2020 Side 17
Madan Mohan Malaviya Univ. of Technology, Gorakhpur
2. Data types
Sequence Type: list …continued
25-09-2020 Side 18
Madan Mohan Malaviya Univ. of Technology, Gorakhpur
2. Data types
Sequence Type: tuple
Tuple is a collection which is ordered, indexed and unchangeable. It allows
duplicate members.
Tuples are written with round brackets ( ).
We can access tuple items by referring to the index number, inside square
brackets.
25-09-2020 Side 19
Madan Mohan Malaviya Univ. of Technology, Gorakhpur
2. Data types
Sequence Type: tuple …continued
25-09-2020 Side 20
Madan Mohan Malaviya Univ. of Technology, Gorakhpur
2. Data types
Set Type: set
Set is a collection which is unordered, unindexed and unchangeable. It does
not allow duplicate members.
Sets are written with curly brackets { }.
25-09-2020 Side 21
Madan Mohan Malaviya Univ. of Technology, Gorakhpur
2. Data types
Mapping Type (Dictionary): dict
Dictionary is a collection which is unordered, indexed and changeable. It does
not allow duplicate members.
Dictionaries are written with curly brackets, and they have keys and values.
We can access the items of a dictionary by referring to its key name, inside square
brackets.
25-09-2020 Side 22
Madan Mohan Malaviya Univ. of Technology, Gorakhpur
3. Variables
25-09-2020 Side 23
Madan Mohan Malaviya Univ. of Technology, Gorakhpur
3. Variables
Declaration and initialization
Variables are containers for storing data values.
Python has no command for declaring a variable.
A variable is created the moment you first assign a value to it.
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)
Variables do not need to be declared with any particular type and can even change type
after they have been set.
String variables can be declared either by using single or double quotes.
We can assign values to multiple variables in one line.
We can assign the same value to multiple variables in one line.
25-09-2020 Side 24
Madan Mohan Malaviya Univ. of Technology, Gorakhpur
3. Variables
Declaration and initialization …continued
25-09-2020 Side 25
Madan Mohan Malaviya Univ. of Technology, Gorakhpur
4. Simple Statements
25-09-2020 Side 26
Madan Mohan Malaviya Univ. of Technology, Gorakhpur
4. Simple Statements
Taking inputs from user
Python 3.6 uses the input( ) method while Python 2.7 uses the raw_input( ) method.
25-09-2020 Side 27
Madan Mohan Malaviya Univ. of Technology, Gorakhpur
4. Simple Statements
Displaying outputs
print statement is often used to output variables.
To concatenate two or more strings or string variables the + character is used.
For numbers, the + character works as a mathematical operator.
If you try to combine a string and a number, Python will give you an error.
25-09-2020 Side 28
Madan Mohan Malaviya Univ. of Technology, Gorakhpur
5. Other Concepts
25-09-2020 Side 29
Madan Mohan Malaviya Univ. of Technology, Gorakhpur
5. Other Concepts
Operators and Expressions
•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
Madan Mohan Malaviya Univ. of Technology, Gorakhpur
5. Other Concepts
Operators and Expressions …continued
Arithmetic operators
** is used for Exponentiation and // is used for Floor division.
25-09-2020 Side 31
Madan Mohan Malaviya Univ. of Technology, Gorakhpur
5. Other Concepts
Operators and Expressions …continued
Logical operators
25-09-2020 Side 32
Madan Mohan Malaviya Univ. of Technology, Gorakhpur
5. Other Concepts
Operators and Expressions …continued
Identity operators
25-09-2020 Side 33
Madan Mohan Malaviya Univ. of Technology, Gorakhpur
5. Other Concepts
Operators and Expressions …continued
Identity operators
25-09-2020 Side 34
Madan Mohan Malaviya Univ. of Technology, Gorakhpur
5. Other Concepts
Operators and Expressions …continued
Membership operators
25-09-2020 Side 35
Madan Mohan Malaviya Univ. of Technology, Gorakhpur
5. Other Concepts
Indentation
Indentation refers to the spaces at the beginning of a code line.
Where in other programming languages the indentation in code is for readability only,
the indentation in Python is very important.
Python uses indentation to indicate a block of code. Other languages often use curly-
brackets for this purpose.
Python will give you an error if you skip the indentation.
The number of spaces is up to you as a programmer, but it has to be at least one.
You have to use same number of spaces in the same block of code, otherwise Python
will give you an error.
Correct Example:
if 5 > 2:
print("Five is greater than two!")
if 5 > 2:
print("Five is greater than two!")
25-09-2020 Side 36
Madan Mohan Malaviya Univ. of Technology, Gorakhpur
5. Other Concepts
Comments
Comments starts with a #, and Python will ignore them.
Python does not really have a syntax for multi line comments.
To add a multiline comment you could insert a # for each line.
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
Madan Mohan Malaviya Univ. of Technology, Gorakhpur
5. Other Concepts
Casting
Casting can be done using constructors : int( ), float( ), str( )
25-09-2020 Side 38
Madan Mohan Malaviya Univ. of Technology, Gorakhpur
6. Conditional Statements
25-09-2020 Side 39
Madan Mohan Malaviya Univ. of Technology, Gorakhpur
6. Conditional Statements
if…elif…else
a = 200
b = 33
if b > a:
print("b is greater than a")
elif a == 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.
if a > b: print("a is greater than b")
25-09-2020 Side 40
Madan Mohan Malaviya Univ. of Technology, Gorakhpur
6. Conditional Statements
if…elif…else
25-09-2020 Side 41
Madan Mohan Malaviya Univ. of Technology, Gorakhpur
6. Conditional Statements
if…elif…else
Ternary Operators, or Conditional 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") if a > b else print("B")
You can also have multiple else statements on the same line.
a = 330
b = 330
print("A") if a > b else print("=") if a == b else print("B")
Logical operators to combine conditional statements
The and/or keywords are logical operators, and can be used as below:
if a > b and a > c:
if a > b or a > c:
25-09-2020 Side 42
Madan Mohan Malaviya Univ. of Technology, Gorakhpur
6. Conditional Statements
Ternary operator
25-09-2020 Side 43
Madan Mohan Malaviya Univ. of Technology, Gorakhpur
6. Conditional Statements
Logical operator
25-09-2020 Side 44
Madan Mohan Malaviya Univ. 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
if x > 10:
print("Above ten,")
if x > 20:
print("and also above 20!")
else:
print("but not above 20.")
25-09-2020 Side 45
Madan Mohan Malaviya Univ. of Technology, Gorakhpur
6. Conditional Statements
Nested if
25-09-2020 Side 46
Madan Mohan Malaviya Univ. 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
if b > a:
pass
25-09-2020 Side 47
Madan Mohan Malaviya Univ. of Technology, Gorakhpur
6. Conditional Statements
Pass statement
25-09-2020 Side 48
Madan Mohan Malaviya Univ. of Technology, Gorakhpur
Queries ?

More Related Content

PDF
Python Programming Unit 2 - Introduction
PDF
Python Programming Unit 4 Problem Solving and Data Analysis
PDF
Python programming report demo for all the students
PDF
COVID-19 CASES PREDICTION USING MACHINE LEARNING
PDF
Post Graduate Certificate in Software Engineering for Data Science
PDF
IRJET- Online Programming Assessment and Evaluation Platform in Education System
PDF
IRJET - Fake News Detection using Machine Learning
Python Programming Unit 2 - Introduction
Python Programming Unit 4 Problem Solving and Data Analysis
Python programming report demo for all the students
COVID-19 CASES PREDICTION USING MACHINE LEARNING
Post Graduate Certificate in Software Engineering for Data Science
IRJET- Online Programming Assessment and Evaluation Platform in Education System
IRJET - Fake News Detection using Machine Learning

Similar to To get the notes of python of MCA batchs (20)

PPTX
A Rubric For District Robotics Success: A Buyer's Guide & Hands On Experience...
PPTX
GEETHAhshansbbsbsbhshnsnsn_INTERNSHIP.pptx
PDF
Te computer-syllabus-2015-course-3-4-17
PDF
PDF
Programmingwithc 131017034813-phpapp01
DOCX
Internship report on AI , ML & IIOT and project responses full docs
PDF
AI and Web-Based Interactive College Enquiry Chatbot
PPTX
9.8.24_operators........................
PDF
ravish m
PDF
Se be information technology rev 2016
PDF
Se be information technology rev 2016
PDF
IRJET- QUEZARD : Question Wizard using Machine Learning and Artificial Intell...
PPTX
CSEIT- ALL.pptx
PDF
CSE NEW_4th yr w.e.f. 2018-19.pdf
PDF
Advanced Software Engineering Program with IIT Madras
PDF
C++chapter2671
PDF
Detecting Malicious Bots in Social Media Accounts Using Machine Learning Tech...
PDF
IRJET- Lost: The Horror Game
PDF
Te computer syllabus 2015 course 3-4-17 3-5-17
PDF
BIT211_2.pdf
A Rubric For District Robotics Success: A Buyer's Guide & Hands On Experience...
GEETHAhshansbbsbsbhshnsnsn_INTERNSHIP.pptx
Te computer-syllabus-2015-course-3-4-17
Programmingwithc 131017034813-phpapp01
Internship report on AI , ML & IIOT and project responses full docs
AI and Web-Based Interactive College Enquiry Chatbot
9.8.24_operators........................
ravish m
Se be information technology rev 2016
Se be information technology rev 2016
IRJET- QUEZARD : Question Wizard using Machine Learning and Artificial Intell...
CSEIT- ALL.pptx
CSE NEW_4th yr w.e.f. 2018-19.pdf
Advanced Software Engineering Program with IIT Madras
C++chapter2671
Detecting Malicious Bots in Social Media Accounts Using Machine Learning Tech...
IRJET- Lost: The Horror Game
Te computer syllabus 2015 course 3-4-17 3-5-17
BIT211_2.pdf
Ad

Recently uploaded (20)

DOCX
mcsp232projectguidelinesjan2023 (1).docx
PDF
Blue-Modern-Elegant-Presentation (1).pdf
PPTX
PMP (Project Management Professional) course prepares individuals
PPTX
The Stock at arrangement the stock and product.pptx
PDF
Prostaglandin E2.pdf orthoodontics op kharbanda
PPTX
Autonomic_Nervous_SystemM_Drugs_PPT.pptx
PDF
Biography of Mohammad Anamul Haque Nayan
PDF
Daisia Frank: Strategy-Driven Real Estate with Heart.pdf
PDF
APNCET2025RESULT Result Result 2025 2025
PPTX
DPT-MAY24.pptx for review and ucploading
PPTX
FINAL PPT.pptx cfyufuyfuyuy8ioyoiuvy ituyc utdfm v
PDF
L-0018048598visual cloud book for PCa-pdf.pdf
PPTX
退学买新西兰毕业证(WelTec毕业证书)惠灵顿理工学院毕业证国外证书制作
PPTX
Surgical thesis protocol formation ppt.pptx
PDF
esg-supply-chain-webinar-nov2018hkhkkh.pdf
PPTX
_+✅+JANUARY+2025+MONTHLY+CA.pptx current affairs
PPTX
AREAS OF SPECIALIZATION AND CAREER OPPORTUNITIES FOR COMMUNICATORS AND JOURNA...
DOC
field study for teachers graduating samplr
PPTX
PE3-WEEK-3sdsadsadasdadadwadwdsdddddd.pptx
PDF
313302 DBMS UNIT 1 PPT for diploma Computer Eng Unit 2
mcsp232projectguidelinesjan2023 (1).docx
Blue-Modern-Elegant-Presentation (1).pdf
PMP (Project Management Professional) course prepares individuals
The Stock at arrangement the stock and product.pptx
Prostaglandin E2.pdf orthoodontics op kharbanda
Autonomic_Nervous_SystemM_Drugs_PPT.pptx
Biography of Mohammad Anamul Haque Nayan
Daisia Frank: Strategy-Driven Real Estate with Heart.pdf
APNCET2025RESULT Result Result 2025 2025
DPT-MAY24.pptx for review and ucploading
FINAL PPT.pptx cfyufuyfuyuy8ioyoiuvy ituyc utdfm v
L-0018048598visual cloud book for PCa-pdf.pdf
退学买新西兰毕业证(WelTec毕业证书)惠灵顿理工学院毕业证国外证书制作
Surgical thesis protocol formation ppt.pptx
esg-supply-chain-webinar-nov2018hkhkkh.pdf
_+✅+JANUARY+2025+MONTHLY+CA.pptx current affairs
AREAS OF SPECIALIZATION AND CAREER OPPORTUNITIES FOR COMMUNICATORS AND JOURNA...
field study for teachers graduating samplr
PE3-WEEK-3sdsadsadasdadadwadwdsdddddd.pptx
313302 DBMS UNIT 1 PPT for diploma Computer Eng Unit 2
Ad

To get the notes of python of MCA batchs

  • 1. 25-09-2020 Side 1 Madan Mohan Malaviya Univ. of Technology, Gorakhpur PROGRAMMING IN PYTHON MCA-161A 4 Credits (3-0-2) MCA 5th Sem (2020-21) R K Dwivedi Assistant Professor Department of ITCA MMMUT Gorakhpur
  • 2. 25-09-2020 Side 2 Madan Mohan Malaviya Univ. of Technology, Gorakhpur Programming Basics and Decision Making 1. Introduction: Key features and applications of Python, Python Editors and Compilers (Interpreters), Using different offline and online Python IDE, Interacting with Python programs 2. Data types: Numeric, Boolean, Strings, Lists, Tuples, Sets, Dictionary 3. Variables: Declaration and initialization 4. Simple Statements: Taking inputs from user, Displaying outputs 5. Other concepts: Operators, Expressions, Indentation, Comments, Casting 6. Conditional statements: if…elif…else Contents
  • 3. 25-09-2020 Side 3 Madan Mohan Malaviya Univ. of Technology, Gorakhpur 1. Introduction
  • 4. 25-09-2020 Side 4 Madan Mohan Malaviya Univ. of Technology, Gorakhpur 1. Introduction Key features • It is most widely used, high-level, general-purpose, multi-purpose and interpreted programming language. • There are two major Python versions: Python 2 and Python 3 (latest version is 3.7.1, we can simply call it as Python 3). • It was created by Guido Van Rossum, and released in 1991. Python 3.0 was released in 2008. • It is developed under an OSI-approved open source license, making it freely usable and distributable, even for commercial use. • 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 interpreter system, meaning that code can be executed as soon as it is written. • Python can be treated in a procedural way, an object-orientated way or a functional way. • Its design philosophy emphasizes code readability. • Its syntax allows programmers to express concepts in fewer lines of code. • The best way we learn anything is by practice and exercise questions. • Python uses new lines to complete a command instead of semicolons or parentheses. • Python uses indentation through whitespace instead of curly-brackets, to define the scope of loops, functions and classes. • Python can connect to database systems. 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 in many applications (NumPy for numerical calculations, Pandas for data analytics etc). • you can download it for free from the following website: https://www.python.org/
  • 5. 25-09-2020 Side 5 Madan Mohan Malaviya Univ. of Technology, Gorakhpur 1. Introduction Applications of Python • Python can be used on a server to create web applications. • It can be used to create GUI based desktop applications(Games, Scientific and Business Applications). • It is also used to create test frameworks and multimedia applications. • It is used to develop operating systems and programming language. • 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 data and 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.
  • 6. 25-09-2020 Side 6 Madan Mohan Malaviya Univ. of Technology, Gorakhpur 1. Introduction Python Editors and Interpreters • Python is interpreted language i.e it’s not compiled and the interpreter will check the code line by line. • Code can be written in a text editor with .py extension and then it can be put into the python interpreter for execution.
  • 7. 25-09-2020 Side 7 Madan Mohan Malaviya Univ. of Technology, Gorakhpur 1. Introduction Using different offline and online Python IDE Offline IDE: Thonny, Pycharm, Spyder, IDLE, Netbeans or Eclipse PyDev etc. Online IDE: 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
  • 8. 25-09-2020 Side 8 Madan Mohan Malaviya Univ. of Technology, Gorakhpur 1. Introduction Interacting with Python programs
  • 9. 25-09-2020 Side 9 Madan Mohan Malaviya Univ. of Technology, Gorakhpur 2. Datatypes
  • 10. 25-09-2020 Side 10 Madan Mohan Malaviya Univ. of Technology, Gorakhpur 2. Data types Python is dynamically typed language(No need to mention data type based on value assigned, it takes data type) You can get the data type of any object by using the type( ) function
  • 11. 25-09-2020 Side 11 Madan Mohan Malaviya Univ. of Technology, Gorakhpur 2. Data types Numeric Type: int, float, complex
  • 12. 25-09-2020 Side 12 Madan Mohan Malaviya Univ. of Technology, Gorakhpur 2. Data types Boolean Type: bool
  • 13. 25-09-2020 Side 13 Madan Mohan Malaviya Univ. of Technology, Gorakhpur 2. Data types Text Type (Strings): str We can assign a string to a variable as below: a = "Hello“ We can assign a multiline string to 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 strings and numbers like this: age = 30 txt = "My name is XYZ, I am " + age
  • 14. 25-09-2020 Side 14 Madan Mohan Malaviya Univ. of Technology, Gorakhpur 2. Data types Text Type (Strings): str …continued
  • 15. 25-09-2020 Side 15 Madan Mohan Malaviya Univ. of Technology, Gorakhpur 2. Data types Text Type (Strings): str …continued
  • 16. 25-09-2020 Side 16 Madan Mohan Malaviya Univ. of Technology, Gorakhpur 2. Data types Sequence Type: list List is a collection which is ordered, indexed and changeable. It allows duplicate members. Lists are written with square brackets [ ]. We can access list items by referring to the index number, inside square brackets.
  • 17. 25-09-2020 Side 17 Madan Mohan Malaviya Univ. of Technology, Gorakhpur 2. Data types Sequence Type: list …continued
  • 18. 25-09-2020 Side 18 Madan Mohan Malaviya Univ. of Technology, Gorakhpur 2. Data types Sequence Type: tuple Tuple is a collection which is ordered, indexed and unchangeable. It allows duplicate members. Tuples are written with round brackets ( ). We can access tuple items by referring to the index number, inside square brackets.
  • 19. 25-09-2020 Side 19 Madan Mohan Malaviya Univ. of Technology, Gorakhpur 2. Data types Sequence Type: tuple …continued
  • 20. 25-09-2020 Side 20 Madan Mohan Malaviya Univ. of Technology, Gorakhpur 2. Data types Set Type: set Set is a collection which is unordered, unindexed and unchangeable. It does not allow duplicate members. Sets are written with curly brackets { }.
  • 21. 25-09-2020 Side 21 Madan Mohan Malaviya Univ. of Technology, Gorakhpur 2. Data types Mapping Type (Dictionary): dict Dictionary is a collection which is unordered, indexed and changeable. It does not allow duplicate members. Dictionaries are written with curly brackets, and they have keys and values. We can access the items of a dictionary by referring to its key name, inside square brackets.
  • 22. 25-09-2020 Side 22 Madan Mohan Malaviya Univ. of Technology, Gorakhpur 3. Variables
  • 23. 25-09-2020 Side 23 Madan Mohan Malaviya Univ. of Technology, Gorakhpur 3. Variables Declaration and initialization Variables are containers for storing data values. Python has no command for declaring a variable. A variable is created the moment you first assign a value to it. 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) Variables do not need to be declared with any particular type and can even change type after they have been set. String variables can be declared either by using single or double quotes. We can assign values to multiple variables in one line. We can assign the same value to multiple variables in one line.
  • 24. 25-09-2020 Side 24 Madan Mohan Malaviya Univ. of Technology, Gorakhpur 3. Variables Declaration and initialization …continued
  • 25. 25-09-2020 Side 25 Madan Mohan Malaviya Univ. of Technology, Gorakhpur 4. Simple Statements
  • 26. 25-09-2020 Side 26 Madan Mohan Malaviya Univ. of Technology, Gorakhpur 4. Simple Statements Taking inputs from user Python 3.6 uses the input( ) method while Python 2.7 uses the raw_input( ) method.
  • 27. 25-09-2020 Side 27 Madan Mohan Malaviya Univ. of Technology, Gorakhpur 4. Simple Statements Displaying outputs print statement is often used to output variables. To concatenate two or more strings or string variables the + character is used. For numbers, the + character works as a mathematical operator. If you try to combine a string and a number, Python will give you an error.
  • 28. 25-09-2020 Side 28 Madan Mohan Malaviya Univ. of Technology, Gorakhpur 5. Other Concepts
  • 29. 25-09-2020 Side 29 Madan Mohan Malaviya Univ. of Technology, Gorakhpur 5. Other Concepts Operators and Expressions •Arithmetic operators (+, -, *, /, %, **, //) •Assignment operators (=, +=, -=, *=, /=, %=, **=, //=, &=, |=, <<=, >>=, ^=) •Comparison operators (==, !=, <, >, <=, >=) •Logical operators (and, or, not) •Identity operators (is, is not) •Membership operators (in, not in) •Bitwise operators (&, |, ^, ~, <<, >>)
  • 30. 25-09-2020 Side 30 Madan Mohan Malaviya Univ. of Technology, Gorakhpur 5. Other Concepts Operators and Expressions …continued Arithmetic operators ** is used for Exponentiation and // is used for Floor division.
  • 31. 25-09-2020 Side 31 Madan Mohan Malaviya Univ. of Technology, Gorakhpur 5. Other Concepts Operators and Expressions …continued Logical operators
  • 32. 25-09-2020 Side 32 Madan Mohan Malaviya Univ. of Technology, Gorakhpur 5. Other Concepts Operators and Expressions …continued Identity operators
  • 33. 25-09-2020 Side 33 Madan Mohan Malaviya Univ. of Technology, Gorakhpur 5. Other Concepts Operators and Expressions …continued Identity operators
  • 34. 25-09-2020 Side 34 Madan Mohan Malaviya Univ. of Technology, Gorakhpur 5. Other Concepts Operators and Expressions …continued Membership operators
  • 35. 25-09-2020 Side 35 Madan Mohan Malaviya Univ. of Technology, Gorakhpur 5. Other Concepts Indentation Indentation refers to the spaces at the beginning of a code line. Where in other programming languages the indentation in code is for readability only, the indentation in Python is very important. Python uses indentation to indicate a block of code. Other languages often use curly- brackets for this purpose. Python will give you an error if you skip the indentation. The number of spaces is up to you as a programmer, but it has to be at least one. You have to use same number of spaces in the same block of code, otherwise Python will give you an error. Correct Example: if 5 > 2: print("Five is greater than two!") if 5 > 2: print("Five is greater than two!")
  • 36. 25-09-2020 Side 36 Madan Mohan Malaviya Univ. of Technology, Gorakhpur 5. Other Concepts Comments Comments starts with a #, and Python will ignore them. Python does not really have a syntax for multi line comments. To add a multiline comment you could insert a # for each line. 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.
  • 37. 25-09-2020 Side 37 Madan Mohan Malaviya Univ. of Technology, Gorakhpur 5. Other Concepts Casting Casting can be done using constructors : int( ), float( ), str( )
  • 38. 25-09-2020 Side 38 Madan Mohan Malaviya Univ. of Technology, Gorakhpur 6. Conditional Statements
  • 39. 25-09-2020 Side 39 Madan Mohan Malaviya Univ. of Technology, Gorakhpur 6. Conditional Statements if…elif…else a = 200 b = 33 if b > a: print("b is greater than a") elif a == 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. if a > b: print("a is greater than b")
  • 40. 25-09-2020 Side 40 Madan Mohan Malaviya Univ. of Technology, Gorakhpur 6. Conditional Statements if…elif…else
  • 41. 25-09-2020 Side 41 Madan Mohan Malaviya Univ. of Technology, Gorakhpur 6. Conditional Statements if…elif…else Ternary Operators, or Conditional 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") if a > b else print("B") You can also have multiple else statements on the same line. a = 330 b = 330 print("A") if a > b else print("=") if a == b else print("B") Logical operators to combine conditional statements The and/or keywords are logical operators, and can be used as below: if a > b and a > c: if a > b or a > c:
  • 42. 25-09-2020 Side 42 Madan Mohan Malaviya Univ. of Technology, Gorakhpur 6. Conditional Statements Ternary operator
  • 43. 25-09-2020 Side 43 Madan Mohan Malaviya Univ. of Technology, Gorakhpur 6. Conditional Statements Logical operator
  • 44. 25-09-2020 Side 44 Madan Mohan Malaviya Univ. 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 if x > 10: print("Above ten,") if x > 20: print("and also above 20!") else: print("but not above 20.")
  • 45. 25-09-2020 Side 45 Madan Mohan Malaviya Univ. of Technology, Gorakhpur 6. Conditional Statements Nested if
  • 46. 25-09-2020 Side 46 Madan Mohan Malaviya Univ. 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 if b > a: pass
  • 47. 25-09-2020 Side 47 Madan Mohan Malaviya Univ. of Technology, Gorakhpur 6. Conditional Statements Pass statement
  • 48. 25-09-2020 Side 48 Madan Mohan Malaviya Univ. of Technology, Gorakhpur Queries ?