Python UNIT 1
Python UNIT 1
Contents
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
Side 1
BBS GROUP OF INSTITUTIONS
1. Introduction
Side 2
BBS GROUP OF INSTITUTIONS
1. Introduction
Key features
• It is the most widely used, high-level, general-purpose, multi-purpose, and interpreted programming language.
• There are two major Python versions: Python 2 and Python 3 (the latest version is 3.7.1, we can call it 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, object-orientated, or 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 well suited for beginners and experienced programmers with other programming languages like C++
and Java.
• The biggest strength of Python is the 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
BBS GROUP OF INSTITUTIONS
1. Introduction
Applications of Python
1. Introduction
Side 5
BBS GROUP OF INSTITUTIONS
1. Introduction
https://www.tutorialspoint.com/execute_python_onli
ne.php
Side 6
BBS GROUP OF INSTITUTIONS
1. Introduction
Side 7
BBS GROUP OF INSTITUTIONS
2. Datatypes
Side 8
BBS GROUP OF INSTITUTIONS
2. Data types
Python is a 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 9
BBS GROUP OF INSTITUTIONS
2. Data types
Numeric Type: int, float, complex
Side 10
BBS GROUP OF INSTITUTIONS
2. Data types
Boolean Type: bool
Side 11
BBS GROUP OF INSTITUTIONS
2. Data types
Text Type (Strings): str
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.”””
Side 12
BBS GROUP OF INSTITUTIONS
2. Data types
Text Type (Strings): str …continued
Side 13
BBS GROUP OF INSTITUTIONS
2. Data types
Text Type (Strings): str …continued
Side 14
BBS GROUP OF INSTITUTIONS
2. Data types
Sequence Type: list
A list is a collection that 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 15
BBS GROUP OF INSTITUTIONS
2. Data types
Sequence Type: list …continued
Side 16
BBS GROUP OF INSTITUTIONS
2. Data types
Sequence Type: tuple
A tuple is a collection that 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 17
BBS GROUP OF INSTITUTIONS
2. Data types
Sequence Type: tuple …continued
Side 18
BBS GROUP OF INSTITUTIONS
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 19
BBS GROUP OF INSTITUTIONS
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 20
BBS GROUP OF INSTITUTIONS
3. Variables
Side 21
BBS GROUP OF INSTITUTIONS
3. Variables
Declaration and initialization
Variables do not need to be declared with any particular type and can even
change type after they have been set.
Side 22
BBS GROUP OF INSTITUTIONS
3. Variables
Declaration and initialization …continued
Side 23
BBS GROUP OF INSTITUTIONS
4. Simple Statements
Side 24
BBS GROUP OF INSTITUTIONS
4. Simple Statements
Taking inputs from user
Python 3.6 uses the input( ) method while Python 2.7 uses the
raw_input( ) method.
Side 25
BBS GROUP OF INSTITUTIONS
4. Simple Statements
Displaying outputs
Side 26
BBS GROUP OF INSTITUTIONS
5. Other Concepts
Side 27
BBS GROUP OF INSTITUTIONS
5. Other Concepts
Side 28
BBS GROUP OF INSTITUTIONS
5. Other Concepts
Operators and Expressions …continued
Arithmetic operators
** is used for Exponentiation and // is used for
Floor division.
Side 29
BBS GROUP OF INSTITUTIONS
5. Other Concepts
Operators and Expressions …continued
Logical operators
Side 30
BBS GROUP OF INSTITUTIONS
5. Other Concepts
Operators and Expressions …continued
Identity operators
Side 31
BBS GROUP OF INSTITUTIONS
5. Other Concepts
Operators and Expressions …continued
Identity operators
Side 32
BBS GROUP OF INSTITUTIONS
5. Other Concepts
Operators and Expressions …continued
Membership operators
Side 33
BBS GROUP OF INSTITUTIONS
5. Other Concepts
Indentation
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 34
BBS GROUP OF INSTITUTIONS
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 35
BBS GROUP OF INSTITUTIONS
Queries ?
Side 36