Getting Started with Python Basics
Getting Started with Python Basics
This document contains valuable confidential and proprietary information of ABESEC. Such confidential and
proprietary information includes, amongst others, proprietary intellectual property which can be legally protected and
commercialized. Such information is furnished herein for training purposes only. Except with the express prior
written permission of ABESEC, this document and the information contained herein may not be published,
disclosed, or used for any other purpose.
2
Copyright © 2021, ABES Engineering College
Topics Covered
Day 5 Day 6
4
Copyright © 2021, ABES Engineering College
Session Plan - Day 1
5
Copyright © 2021, ABES Engineering College
Introduction
Python is a Popular
Programming language
?
[Link]
6
Copyright © 2021, ABES Engineering College
Some Areas where it is Popular
❑ Application Development
❑ Web Development
❑ Artificial Intelligence
❑ Data Science
7
Copyright © 2021, ABES Engineering College
Why choose Python : Lets Discuss More
8
Copyright © 2021, ABES Engineering College
Scope of Python
❑ Python Language provides promising and rewarding career in the IT industry
❑ Various Job roles advanced in Python with high paying jobs:
❑ Research Analyst
❑ DevOps Engineer
❑ Python Developer
❑ Data Analyst
❑ Software Developer
❑ Game Developer
❑ Web Scrapper
9
Copyright © 2021, ABES Engineering College
Python History
Python was written in the late 1980s by Guido van Rossum at Centrum
Wiskunde & Informatica (CWI)
10
Copyright © 2021, ABES Engineering College
Python Features
11
Copyright © 2021, ABES Engineering College
Review Questions
12
Copyright © 2021, ABES Engineering College
Review Questions
13
Copyright © 2021, ABES Engineering College
Session Plan - Day 2
14
Copyright © 2021, ABES Engineering College
Advantages of Python
15
Copyright © 2021, ABES Engineering College
Contd..
16
Copyright © 2021, ABES Engineering College
Contd..
In Python
17
Copyright © 2021, ABES Engineering College
Disadvantages of Python
❑ Python code is executed line by line since Python is interpreted as slower
in runtime than other programming languages like C++, Java, and PHP.
❑ Python takes a lot of memory due to the flexibility of the data types, so it
is not a desirable choice for memory-intensive tasks.
❑ Although Python serves as an excellent server-side programming
language, it is less commonly used to build intelligent phone-based
Applications.
❑ Python is rarely used in Enterprise development because Python has
some limitations with Database Access compared to primarily other used
technologies like JDBC (Java Database Connectivity and ODBC (Open
Database Connectivity) as Python Language Database layers are
underdeveloped.
18
Copyright © 2021, ABES Engineering College
Your Future….!!!
20
Copyright © 2021, ABES Engineering College
Different Flavors of Python
❑ Cpython
❑ Jpython
❑ Active Python
❑ Anaconda Python
❑ PyPy
❑ Win Python
❑ Python Portable
21
Copyright © 2021, ABES Engineering College
Different Python Frameworks
❑ Full-Stack Frameworks
❑ Non-Full Stack Frameworks
❑ Asynchronous Frameworks
22
Copyright © 2021, ABES Engineering College
Review Questions
How to output the string “May the odds favor you” in Python?
▪ print(“May the odds favor you”)
▪ echo(“May the odds favor you”)
▪ [Link](“May the odds favor you”)
▪ printf(“May the odds favor you”)
23
Copyright © 2021, ABES Engineering College
Review Questions
24
Copyright © 2021, ABES Engineering College
Quality Content for Outcome based Learning
26
Copyright © 2021, ABES Engineering College
1.2 Python Installation Guide
Before you start, you will need Python on your computer.
Installing Python on your computer is the first step to becoming a Python
programmer.
Python has two main versions:
• Python 2
• Python 3
However, Python installation differs among different operating systems. The use
of Python 3 is highly preferred over Python 2.
27
Copyright © 2021, ABES Engineering College
Contd..
For the installation process, go to the official website of Python, i.e.,
[Link]. Refer to the current stable version 3.9.4 as of date 13 April
[Link] will get the installer for Python 3.7 or Python 3.9. (at the time of
writing). You may even have it with a 32-bit or 64-bit processor versions.
28
Copyright © 2021, ABES Engineering College
1.2.1 Introduction to python IDE – IDLE
If you have recently installed Python on your computer, you might have seen a
new IDLE program.
"What is this software doing on my computer?" you might be curious. I did not
download that!"
Though you might not have downloaded IDLE on your own, it is included with
any Python installation. It is there to help you get acquainted with the language
right away.
29
Copyright © 2021, ABES Engineering College
Contd..
30
Copyright © 2021, ABES Engineering College
Contd..
Alternate way: You can also open a terminal window and run the interpreter
from the command line.
It is known as Command Prompt in Windows. It can be renamed terminal in
macOS or Linux.
You can type Windows key+ R and type cmd to open Command Prompt.
Then, type python to execute python programs.
31
Copyright © 2021, ABES Engineering College
Contd..
Start working with Python shell.
To print () to display the string "ABES Engineering College" on your computer.
Enter the command one at a time, and Python returns the results of each
command.
32
Copyright © 2021, ABES Engineering College
Contd..
From this menu bar, you can restart the shell. It will behave as if you had
launched a new instance of Python IDLE. The shell will forget anything from its
former state.
If you want to exit the interpreter, then type exit () and press Enter.
33
Copyright © 2021, ABES Engineering College
Python IDLE Editor
Python IDLE includes a full-featured file editor, allowing you to write and run
Python programs. The built-in file editor also supplies many tools to speed up
your coding workflows, such as code completion and automated indentation.
Select File “New File” from the menu bar to begin a new Python file
34
Copyright © 2021, ABES Engineering College
Contd..
When you are ready to work on a file, click the Edit button. You can save the file
as [Link] (.py is an extension to save python scripts files) in the
specified default location "C:\Users\Aatif\AppData\Local\Programs\Python."
35
Copyright © 2021, ABES Engineering College
Contd..
When you want to run a file, you must first ensure that it has been saved,
remember to check for asterisks * around the filename at the top of the file
editor window to see whether the file was correctly saved.
But do not panic if you forget! When you want to run an unsaved file in Python
IDLE, it will prompt you to save it.
Click the F5 key on your keyboard to run a file in IDLE. You can also use the
menu bar to choose Run Module
36
Copyright © 2021, ABES Engineering College
1.2.2 Setting Up Your Environment
The route(path that lists the directories in which the OS (Operating System)
looks for executables) is saved in an environment variable called a [Link]
variable holds knowledge that the command shell and other programs can use.
37
Copyright © 2021, ABES Engineering College
Contd..
So, navigate to
Control Panel –> System
–> Advanced System
Settings–> Environment
Variables and choose
the PATH variable
38
Copyright © 2021, ABES Engineering College
Contd..
Add the Python path to the end of the string, this is where the package
management software, unit testing tools, and other command line-accessible
Python programs can live.
C:\Users\Aatif\AppData\Local\Programs\Python\Python37-32\Scripts\
39
Copyright © 2021, ABES Engineering College
1.2.3 Installation of Anaconda Navigator
Anaconda Navigator is a desktop graphical user interface that comes with
Anaconda, which allows you to open programs and control conda packages,
environments, and networks without needing to use a command-line interface.
40
Copyright © 2021, ABES Engineering College
Contd..
Click on install and Choose destination folder for installation.
41
Copyright © 2021, ABES Engineering College
1.2.4 Quick Tour of Jupyter Notebook
In an earlier topic, we have seen the installation of the Anaconda package.
Jupyter comes by default with this package
42
Copyright © 2021, ABES Engineering College
Contd..
Alternate way: If you type 'Jupyter notebook' into your command prompt, it will
open the Jupyter dashboard for you
43
Copyright © 2021, ABES Engineering College
Contd..
You might have found that the URL for the dashboard is
[Link] while Jupyter Notebook is open in your window. The
term "localhost" does not refer to a website, but to the fact that the content is
served from your own devices.
44
Copyright © 2021, ABES Engineering College
Contd..
If you want to create your first file, then you must click on new and then python3.
When you return to the dashboard, you can see the new file [Link]
with green boundary in cell.
A cell is a container for the text that will be viewed in the notebook or code that
the notebook's kernel will execute.
45
Copyright © 2021, ABES Engineering College
Contd..
Every ipynb file stands for a single notebook, which means that each time you
create a new notebook, a [Link] file is generated .You should be aware of
kernel that are unfamiliar to you.
A kernel is a kind of "computational engine" that runs the code in a notebook
paper.
46
Copyright © 2021, ABES Engineering College
Contd..
Shift + Enter is the shortcut command to run your cell.
The green boundary over the cell shows the editable mode, and the Blue
boundary over the cell shows the command mode.
47
Copyright © 2021, ABES Engineering College
1.2.5 Python vs. IPython
IPython's interactive shell is known as Ipython.
IPython is a Python graphical command-line terminal founded by Fernando
Perez in 2001. IPython supplies an improved read-eval-print loop (REPL)
environment that is particularly well suited to scientific computing.
48
Copyright © 2021, ABES Engineering College
Contd..
IPython is interactive, and it gives some relaxation to the eyes of coders by
introducing some colors. Some useful commands are not present with the
existing Python idle.
The following are the valuable commands:
• %pwd
• %ls
• %History
49
Copyright © 2021, ABES Engineering College
Contd..
We can also check methods associated with data structures by pressing the tab
over the keyboard.
50
Copyright © 2021, ABES Engineering College
1.2.6 Online compilation support
Assume your machine lacks the necessary resources to install, but you need to
learn Python or run code to try something.
You will need a browser, which you already have. Using online IDEs saves yor
time in the configuration process.
51
Copyright © 2021, ABES Engineering College
1.2.6 Online compilation support
There are various python online editors available in an open market. Here are
the few editors:
Programiz([Link]
52
Copyright © 2021, ABES Engineering College
Contd..
w3schools([Link]
53
Copyright © 2021, ABES Engineering College
Contd..
Onlinegdb ([Link]
54
Copyright © 2021, ABES Engineering College
Contd..
55
Copyright © 2021, ABES Engineering College
Review Questions
56
Copyright © 2021, ABES Engineering College
Review Questions
57
Copyright © 2021, ABES Engineering College
Session Plan - Day 4
58
Copyright © 2021, ABES Engineering College
Python keywords
Python keywords are special reserved words that have specific meanings
and purposes.
❑ function name
❑ variable name
❑ identifiers
59
Copyright © 2021, ABES Engineering College
Contd..
60
Copyright © 2021, ABES Engineering College
Python Statement and Comments
Python Statement –
❑ In Python Programming, any executable instruction, that tell the computer to perform a specification
action is refer to as statements.
❑ Program statement can be an
input-output statements,
arithmetic statements,
control statements,
61
Copyright © 2021, ABES Engineering College
Python Statement and Comments
Python Comments –
❑ Comments are a set of statements that are ignored by the python interpreter.
❑ The use of comments makes it easy for humans to understand the source code.
Example –
❑ Multi-line Comments – You can do it other way using a triple quotes, either ''' ''' or """ """ .
Example –
62
Copyright © 2021, ABES Engineering College
Python Literals
Literals are the type of data that is used to store in a variable or constant.
63
Copyright © 2021, ABES Engineering College
String literals
When set of character are enclosed in quotes ( single quotes or double quotes)
then it formed a string literals.
'abes' 'rate_of_interest'
Example - '123', '12.5'
"ABESEC" "Simple_interest"
"A1" "456 "
Example -
'ABES Engineering "ABES Engineering
College \ College \
NH-24 Delhi Hapur NH-24 Delhi Hapur
Bypass \ Bypass \
Near Crossing Republic' Near Crossing Republic"
64
Copyright © 2021, ABES Engineering College
Numeric Literals
Numeric literals are of multiple types based on the number type. The types
of numeric literals are integer, float (decimal numbers), and complex
numbers.
Float Literals - These are basically real numbers that consist of both integer
as well as fractional parts.
65
Copyright © 2021, ABES Engineering College
Numeric Literals
Example -
10j , 1 + 0j , 10 + 2J, 12 – 5j
66
Copyright © 2021, ABES Engineering College
Boolean Literals , Special literals
Boolean Literals - True or False are the values to be used as the Boolean
values.
In Python, True represents the Non-zero value and False represents the
value as Zero.
67
Copyright © 2021, ABES Engineering College
Literal Collections
List Literals – List is a set of values of different types. The values are
separated by comma (,) and enclosed within square brackets( [ ]).
Tuple literals – A tuple is a set of values of different types. The values are
separated by comma(,) and enclosed within parentheses “ ( ) “. It is
immutable.
Example -
( 1, 23, 23.4, 100 )
Set literals – Set is a collection of values of different types. The values are
separated by comma (,) and enclosed within curly-braces “{ }”. It is
unordered and contains only unique value.
Example – a = 23
Here ‘a’, ‘name’ and ‘Marks’ are variable which
name = 'Ram'
refer an integer value 23, a string ‘Ram’ and float
Value 23.5.
Marks = 23.5
70
Copyright © 2021, ABES Engineering College
Variables
Rules for variable name –
❑ Variables can be named with an alpha-numeric combination, started with
an alphabet or underscore.
❑ Variable name can’t start with digit.
❑ Multi- word space separated name can’t be used as a variable name.
❑ The reserved words(keywords) cannot be used naming the variable.
71
Copyright © 2021, ABES Engineering College
Can you answer these questions?
1) age
2) _age
3) -age
4) age_*
5) Item-Number-1
72
Copyright © 2021, ABES Engineering College
type() command
type () command helps in finding the type of the specific declared variable
or a value.
Example –
73
Copyright © 2021, ABES Engineering College
id() command
id () command gives the unique id for a given objects / variable / values.
Note - The unique id is the memory address and will be different each time
when you run for variable or values.
Example –
Unique Identity
74
Copyright © 2021, ABES Engineering College
dir() command
dir () command is a vital function that returns all the properties and
methods associated with given objects.
Example –
75
Copyright © 2021, ABES Engineering College
Session Plan - Day 5
76
Copyright © 2021, ABES Engineering College
Type conversion
The process of converting the value of one data type (e.g. integer, string,
float, etc.) to another data type is called type conversion.
Example -
12.5 12 ( float to integer )
'123' 123 ( string to integer )
12 12.0 ( integer to float )
77
Copyright © 2021, ABES Engineering College
Type conversion : Implicit Type Conversion
Example –
Note - Python promotes the conversion of the lower data type (integer) to
the higher data type (float) to avoid data loss.
78
Copyright © 2021, ABES Engineering College
Type conversion : Explicit Type Conversion
Explicit Type Conversion – In this, user converts the data type of variable
of value to needed data type.
Example –
79
Copyright © 2021, ABES Engineering College
Can you answer these questions?
a) 12 (20+0j) 123.45
b) 12 20 123.45
c) Error
80
Copyright © 2021, ABES Engineering College
Basic I/O Operations
81
Copyright © 2021, ABES Engineering College
Basic I/O Operations : input()
Syntax –
input(prompt='')
Prompt - A String, representing a default message
before the input.
Example –
82
Copyright © 2021, ABES Engineering College
Basic I/O Operations : input()
Syntax –
input(prompt='')
Prompt - A String, representing a default message
before the input.
Example –
83
Copyright © 2021, ABES Engineering College
Basic I/O Operations : input()
When we take input using input() function, it return value in string data type.
Example –
84
Copyright © 2021, ABES Engineering College
Basic I/O Operations : print()
print () is a built-in standard function used to print the output to the console.
85
Copyright © 2021, ABES Engineering College
Basic I/O Operations : print()
Example –
86
Copyright © 2021, ABES Engineering College
Basic I/O Operations : print()
Example – Write a Python program that takes two integer as input
from user and print sum of both.
87
Copyright © 2021, ABES Engineering College
Can you answer these questions?
b) 12\n23.5\nABESEC
c)
12
23.5
ABESEC
88
Copyright © 2021, ABES Engineering College
Session Plan - Day 6
89
Copyright © 2021, ABES Engineering College
Operators
Operators are symbol, used to perform mathematical and logical
operation.
90
Copyright © 2021, ABES Engineering College
Arithmetic operators
There are seven arithmetic operators, and these are of:
Operator Meaning Example
Add two operands or x+y
+ unary plus +2
Subtract right operand from the left or x-y
- unary minus -2
* Multiply two operands x*y
Divide left operand by the right one (always results into
/ float)
x/y
Modulus - remainder of the division of left operand by the
% right
x % y (remainder of x/y)
Floor division - division that results into whole number
// adjusted to the left in the number line
x // y
** Exponent - left operand raised to the power of right x**y (x to the power y)
91
Copyright © 2021, ABES Engineering College
Arithmetic operators
Example of all mentioned arithmetic operators
92
Copyright © 2021, ABES Engineering College
Comparison (Relational) Operators
93
Copyright © 2021, ABES Engineering College
Comparison (Relational) Operators
94
Copyright © 2021, ABES Engineering College
Comparison (Relational) Operators
Example –
Note - The == and != operators can actually work with values of any data
type. 95
Copyright © 2021, ABES Engineering College
Bitwise Operators
Bitwise operators act on the bits and performs bit by bit operation on
the operands.
Operator Meaning
Example – Evaluate 2 & 7 & Bitwise AND
| Bitwise OR
How it works - ~ Bitwise NOT
Step 1 – Convert 2 in binary 0010 ^ Bitwise XOR
Step 2 – Convert 7 in binary 0111 >> Bitwise right shift
Step 3 – Perform Bitwise & operation
Step 4 – Convert the result back to << Bitwise left shift
decimal
96
Copyright © 2021, ABES Engineering College
Bitwise Operators – Cont..
Truth Table
Bitwise NOT
A B A&B A|B A^B A ~A
0 1
0 0 0 0 0
1 0
0 1 0 1 1
1 0 0 1 1
1 1 1 1 0
97
Copyright © 2021, ABES Engineering College
Bitwise Operators – Bitwise AND (&)
Example –
Explanation –
98
Copyright © 2021, ABES Engineering College
Bitwise Operators – Bitwise OR( | )
Example –
Explanation –
99
Copyright © 2021, ABES Engineering College
Bitwise Operators – Bitwise XOR ( ^ )
Example –
Explanation –
100
Copyright © 2021, ABES Engineering College
Bitwise Operators – Bitwise Left shift ( << )
Example –
Explanation –
Example –
Explanation –
left. %= x %= 5 x=x%5
//= x //= 5 x = x // 5
**= x **= 5 x = x ** 5
There are various compound
&= x &= 5 x=x&5
operators in Python like a += 5
|= x |= 5 x=x|5
^= x ^= 5 x=x^5
>>= x >>= 5 x = x >> 5
<<= x <<= 5 x = x << 5
103
Copyright © 2021, ABES Engineering College
Logical Operators
Logical operators perform Logical AND, Logical OR and Logical NOT operations.
104
Copyright © 2021, ABES Engineering College
Logical Operators
Example –
105
Copyright © 2021, ABES Engineering College
Identity Operators
Identity Operators – is and is not are the identity operators in Python.
They are used to check if two values (or variables) are located on the same part
of the memory or not.
Operator Meaning
is Gives True if the operands are identical (refer to the same ID or Memory)
is not Gives True if the operands are not identical (do not refer to the ID or Memory)
Example
106
Copyright © 2021, ABES Engineering College
Membership Operators
in and not in are the membership operators in Python.
They are used to test whether a value or variable is found in a sequence (string,
list, tuple, set and dictionary) or not.
Operator Meaning
in Gives True if value/variable is found in the sequence otherwise False
not in Gives True if value/variable is not found in the sequence otherwise True
Example
107
Copyright © 2021, ABES Engineering College
Precedence and associativity
Operator precedence and associativity decide the priorities of the operator.
108
Copyright © 2021, ABES Engineering College
Cont..
Precedence and Associativity Table –
109
Copyright © 2021, ABES Engineering College
print (9 + 2 - 6 * 4)
10 - 4 * 2
print(5 * 2 // 3)
print(5 * (2 // 3))
print(2 ** 3 ** 2)
print((2 ** 3) ** 2)
110
Copyright © 2021, ABES Engineering College
num1, num2, num3 = 2, 3, 4
print ((num1 + num2) * num3)
113
Copyright © 2021, ABES Engineering College
Python 2 vs Python 3
❑ Python has started its journey in 1989-1990 when people started
implementation on it.
❑ In year 2000, python 2.0 came with new features and have a healthy support to
python.
❑ Memory management was the major part evolved in python 2.0.
❑ But in 2008, python has changed in a revolutionary manner to python 3.0.
❑ There was no support of backward compatibility in python 3.0.
114
Copyright © 2021, ABES Engineering College
Python 2 vs Python 3
Let us have a look to the differences between Python 2 and Python 3.
115
Copyright © 2021, ABES Engineering College
Python 2 vs Python 3
Let us have a look to the differences between Python 2 and Python 3.
❑ In for loop, the iterations are used using a xrange function in python 2, which
is replaced by range function in python3.
❑ Some of the libraries which are available in python 2 are not moved in python
3.
❑ Similarly, now the developers are making new libraries for python 3 which are
incompatible to python 2.
116
Copyright © 2021, ABES Engineering College
Summary
❑ Python is an open-source, high level, interpreter- based language that can be used for scientific
and non-scientific computing purposes.
❑ Comments are non-executable statements in a program.
❑ An identifier is a user defined name given to a variable or a constant in a program.
❑ Datatype conversion can happen either explicitly or implicitly.
❑ Operators are constructs that manipulate the value of operands.
❑ Python has input() function for taking user input.
❑ Python has print() function to output data to a standard output device.
❑ There are several data types in Python — integer, boolean, float, complex, string, list, tuple, sets,
None and dictionary.
117
Copyright © 2021, ABES Engineering College
References
1. [Link]
5. [Link]
6. [Link]
7. [Link]
8. [Link]
9. [Link]
118
Copyright © 2021, ABES Engineering College
Thank You
119
Copyright © 2021, ABES Engineering College