python unit1
python unit1
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
Side 1
1. Introduction
Side 2
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/
Side 3
1. Introduction
Applications of Python
Side 4
1. Introduction
Side 5
1. Introduction
https://www.tutorialspoint.com/execute_python_onli
ne.php
Side 6
2. Datatypes
Side 7
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
Side 8
2. Data types
Numeric Type: int, float, complex
Side 9
2. Data types
Boolean Type: bool
Side 10
2. Data types
Text Type (Strings): str
Side 12
2. Data types
Text Type (Strings): str …continued
Side 13
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.
Side 14
2. Data types
Sequence Type: list …continued
Side 15
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.
Side 16
2. Data types
Sequence Type: tuple …continued
Side 17
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 { }.
Side 18
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.
Side 19
3. Variables
Side 20
3. Varia ble s
Decla rat ion and init ializ atio n
Variables do not need to be dec lared with any par ticular t ype and
ca n e ven cha nge type a fter the y ha ve bee n se t.
String va riab les can be dec lare d ei ther by usin g si ngle
Side 22
4. Simple Statements
Side 23
4. Simple Statements
Taking inputs from user
Python 3.6 uses the input( ) method while Python 2.7 uses the
raw_input( ) method.
Side 24
4. Simple Statements
Displaying outputs
Side 25
5. Other Concepts
Side 26
5. Other Concepts
Operators and Expressions
Side 27
5. Other Concepts
Operators and Expressions …continued
Arithmetic operators
** is used for Exponentiation and // is used for
Floor division.
Side 28
5. Other C oncep ts
L og i cal op er ato rs
S id e
29
5. Other Concepts
Operators and Expressions …continued
Identity operators
Side 30
5. Other Concepts
Operators and Expressions …continued
Identity operators
Side 31
5. Other Concepts
Operators and Expressions …continued
Membership operators
Side 32
5. Other Concepts
Indentation
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.
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!")
Side 33
5. Other Concepts
Comments
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.
Side 34
5. Other Concepts
Casting
Side 35
6. Conditional Statements
Side 36
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.
Side 37
6. Conditional Statements
if…elif…else
Side 38
6. Conditional Statements
if…elif…else
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")
if a > b or a > c:
6. Conditional Statements
Ternary operator
6. Conditional Statements
Logical operator
Side 41
6. Conditional Statements
if…elif…else
Nested if
x = 41
if x > 10:
print("Above
ten,") if x > 20:
print("and also above
20!") else:
print("but not above 20.")
Side 42
6. Conditional Statements
Nested if
Side 43
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
Side 44
6. Conditional Statements
Pass statement
Side 45
Queries ?
Side 46