Python
Programming
Lecture 1
Basics of Python
Session Objectives
By the end of this class, you will
understand:
▪ Recognize the difference between compiled and
interpreted languages
▪ Describe the terms Program and Algorithms
▪ Examine the usage of various programming
languages in different domains
▪ Describe evolution of python in depth
▪ Discuss the applications of python in various
domains
▪ Identify various data types and operators in
python
▪ Describe operator associativity in python
Introduction: Why You Should Know Programming?
You understand how
It guarantees you a job
software works
1
2 1
No need for a four-year It teaches you persistence
0
degree
3 9
You learn to combine
You get to work from home
technical skills and
4 8 creativity
You can create anything You develop problem-
you want 5 7 solving skills
6
High income potential Open your own business
Computer Hardware Structure
▪ The hardware structure of computer system is made up of four different essential basic units,
such as:
0 0
1 2
Input unit Output unit
0 0
3 4
Storage Arithmetic logic
unit unit
Compiler Vs Interpreter
S.N
Compiler Interpreter
o
Compiler executes a set of instructions at Interpreter executes only one
1
a time. instruction at a time.
2 Execution is faster. Execution is slower.
It requires more memory for the It requires efficient memory as no
3
generated intermediate object code. intermediate object code is generated.
4 It is difficult to debug. It is easy to debug.
5 Locating an error is not instant. Locating an error is instant.
C, C++ etc are examples of compiled Python, BASIC, Ruby etc are examples
6
languages of interpreted languages
Scans the entire program and translates Translates program one statement at a
7
it as a whole into machine code. time.
Categorizing Languages into Compiled and
Interpreted
Session 1
Activity 1
Time for an activity.
!
Evolution of Programming Languages
What is a Program?
▪ A program is a set of instructions we
give a machine to perform some
actions
▪ It might be easier to understand what
a program is by thinking about a
problem that a program might be built
to solve, and then looking at a
program that would solve that
problem
Building Blocks of a Program
▪ There are some low-level conceptual patterns
that we use to construct programs. These
constructs are not just for Python programs,
they are a part of every programming
language - from machine to high-level
languages.
▪ Input
▪ Function
▪ Classes
▪ Reuse
▪ Execution
- Sequential execution
- Conditional execution
- Repeated execution
▪ Output
What is an Algorithm?
▪ Algorithm refers to a set of rules/instructions that step-by-step define how a work is to be executed
to get the expected results
Characteristics of an Algorithm
▪ Instructions must have the following characteristics to be recognized as an algorithm:
Language
Well-defined inputs Finite-ness
independent
0 0 0 04 05 0
1 2 3 6
Clear and
Well-defined Feasible
unambiguous
outputs
Advantages and Disadvantages of an Algorithm
What is Python?
Guido Van Rossum created Python in
1989, emphasizing the DRY (Don't
Repeat Yourself) concept and
readability
▪ Python is a free and easy-to-learn
programming language
▪ It is high-level language, dynamically
typed, and interpreted
▪ This facilitates error debugging and
supports the rapid development of
application prototypes, establishing it
as the preferred language for
programmers
History of Python
▪ Python is a scripting language that is high-level,
interpreted, interactive, and object-oriented.
Python is intended to be an understandable
language
▪ Python is:
- Interpreted
- Interactive
- Object-oriented
- Beginner's language
▪ Many people believe that the name Python was
inspired by a snake. Even the Python
programming language's logo (given here)
features a blue and yellow snake. The origin
behind the naming, however, is a little different
History of Python
Latest Versions of Python
Session 1
Activity 2
Time for an activity.
!
Some Programming Languages and their Uses
1. Python: Artificial Intelligence and Machine
Learning
▪ Artificial intelligence
▪ Machine learning
▪ Data analytics
2. JavaScript : Rich Interactive Web
It is used to run app across a range
Development
of devices such as
▪ Smartphones
▪ Cloud
▪ Containers
3.▪ Java:
Robust business applications
Enterprise Application Development
▪ Create apps for a range of industries
Some Programming Languages and their Uses
- Banking
- Electronic trading
- E-commerce
- Distributed computing
4. R : Data Analysis
▪ Behavior analysis
▪ Data science
▪ Machine learning projects
5. C/C++ : Operating Systems and System Tools
▪ IoT
▪ Embedded systems
▪ Games
Some Programming Languages and their Uses
6. Golang: Server-Side Programming
▪ Distributed networks
▪ GUI applications
▪ Web applications
7. C# : Application and Web Development
Using .NET
▪ Game development
▪ Server-side programming
▪ Web development
8. PHP : Web Development
A large number of companies use
PHP today to create tools such as
▪ CMS
Some Programming Languages and their Uses
▪ E-commerce platforms
▪ Web applications
9. SQL : Data Management
▪ Design schema
▪ Data retrieval
10. Swift : For Mobile App Development on iOS
▪ Create apps for virtually every
Apple device, such as iPhones,
iPads, Mac Watch and other
devices
Features and Application of Python
Features of Python are as follows:
▪ Simple
▪ Portable
▪ Open Source
▪ Extensible and Embeddable
▪ High-level, Interpreted Language
▪ Large Standard Libraries
▪ Object-oriented
Python is used for a large number of
applications. Some of them are mentioned
below:
▪ Systems Programming
▪ GUIs
▪ Internet Scripting
Features and Application of Python
▪ Component Integration
▪ Database Programming
▪ Rapid Prototyping
▪ Numeric and Scientific Programming
Companies using Python
Top Python IDEs are:
▪ Integrated Development Environments (IDEs) aid in rapid software development and increased
productivity
IDLE PyCharm Visual Studio Code Sublime Text 3 Atom/Atom-IDE
Jupyter Spyder PyDev Thonny
Operators and Variables
▪ Operators are symbols like +, –, =, >, and <
that perform a mathematical or logical operation
on data values to produce a result based on a
set of rules
▪ Operands are the data values that an operator
manipulates
- Consider the following phrase:
- >>> 4 + 6
- where the operands are 4 and 6, and the operator is
+
▪ The Python programming language has a large
number of operators. They really are:
1. Arithmetic Operators
2. Assignment Operators
3. Comparison Operators
4. Logical Operators
5. Bitwise Operators
Operators and Variables
▪ Arithmetic Operators
▪ Addition, subtraction, division, multiplication, and other arithmetic operations are performed using
arithmetic operators. All of the arithmetic operators are listed in the table below
Operators and Variables
▪ Arithmetic Operators 1. >>> 10+35
45
For example,
2. >>> −10+35
25
3. >>> 4*2
8
4. >>> 4**2
16
5. >>> 45/10
4.5
6. >>> 45//10.0
4.0
7. >>> 2025%10
5
8. >>> 2025//10
202
The code above demonstrates arithmetic
operations 1-6
Operators and Variables
▪ Comparison Operators
▪ When two operands' values need to be compared, comparison operators are employed. These
comparison operators always return a Boolean value, which is either True or False. Numbers, Strings,
and Boolean values can all be used as operands. Because ASCII values are used to compare strings
letter by letter, "P" is less than "Q," and "Aston" is greater than "Asher"
Table: List of Comparison
Operators and Variables
▪ Comparison 1. >>>10 == 12
Operators False
For Example: 2. >>>10 != 12
True
Above code illustrates
3. >>>10 < 12
various comparison
operations 1-9 True
4. >>>10 > 12
False
5. >>>10 <= 12
True
6. >>>10 >= 12
False
7. >>> "P" < "Q"
True
8. >>> "Aston" > "Asher"
True
9. >>> True == True
True
Operators and Variables
▪ Logical Operators
▪ The logical operators compare or negate the logical values of their operands and return the logical
value as a consequence
▪ The operands on which the logical operators operate have True or False values. The logical operator
always returns a Boolean value, either True or False
▪ Following table lists all of the logical operators
Operators and Variables
▪ Logical Operators 1. >>> True and False
False
▪ The Truth table for Boolean logic is illustrated 2. >>> True or False
here True
3. >>> not(True) and False
False
4. >>> not(True and False)
True
5. >>> (10 < 0) and (10 > 2)
False
6. >>> (10 < 0) or (10 > 2)
True
7. >>> not(10 < 0) or (10 > 2)
True
For Example:
8. >>> not(10 < 0 or 10 > 2)
False
Above code illustrates various comparison
operations 1-8.
Operators and Variables
▪ Bitwise Operators
▪ Bitwise operators operate on their operands bit by bit, treating them as a sequence of bits (zeroes
and ones). TABLE below lists all of the bitwise operators
The Bitwise Truth table is shown in below
TABLE: Bitwise Truth Table
TABLE: List of Bitwise Operators
Note: The value of p is 60 and q
Operators and Variables
▪ Precedence and Associativity
▪ Operator precedence controls how
operators are parsed in relation to one
another
▪ Higher precedence operators become the
operands of lower precedence operators
▪ The way operators with the same
precedence are parsed is determined by
associativity. Almost all of the operators
are associative from left to right
▪ The precedence of operators is listed in
the TABLE below, starting with the
highest precedence and working down to
the lowest precedence
Table: Operator Precedence in
Python
Operators and Variables
▪ A variable is a named placeholder that
can hold any form of data that the
programme can assign and modify
during execution
▪ Legal Variable Names
▪ Follow the below-mentioned rules for
creating legal variable names in Python
- Any amount of letters, underscores,
and decimals can be used in variable
names
- The name of the variable should not
begin with a number
- Keywords are not permitted as
variable names in Python
- Case matters when it comes to
variable names. Computer and
Computer, for example, are two
separate variables
Operators and Variables
▪ Assigning Values to Variables
▪ The general format for assigning
values to variables is as follows:
- variable_name = expression
- To assign values to variables, the
equal sign (=), commonly known as
the basic assignment operator, is
employed
- The name of the variable is the
operand to the left of the =
operator, and the expression is the
operand to the right of the =
operator, which can be a value or
any code snippet that returns a
value
- On the execution of the assignment
statement, the value is saved in the
variable
Operators and Variables
▪ Assigning Values to Variables
▪ The assignment operator should not be confused with the equal sign (=) in mathematics. Enter the
code below in interactive mode, for example, and see what happens
1. >>> number =100
2. >>> miles
=1000.0
3. >>> name
="Python"
4. >>> number
100
5. >>> miles
1000.0
6. >>> name
'Python'
▪ A variable number is assigned an integer type value in 1, a variable mile is assigned a float type value in
2, and a variable name is assigned a string type value in 3. The values assigned to these variables are
printed in 4, 5, and 6
Operators and Variables
▪ Assigning Values to Variables
▪ In Python, not only the value of a variable but 1. >>> century = 100
also the type of data assigned to it might 2. >>> century
change during programme execution. You can
100
give a variable an integer value, use it as an
integer for a time, and then give it a string 3. >>> century =
value. Any prior assignments are overwritten "hundred"
by a new assignment. As an example, 4. >>> century
'hundred'
▪ In step 1, you assign an integer value to the 1. >>> a = b = c =1
century variable, and in step 3, you assign a
string value to the century variable. In each 2. >>> a
situation, as shown in 2 and 4, different values 1
are printed
3. >>> b
▪ Python allows you to assign the same value to 1
several variables at the same time. As an 4. >>> c
example,
1
▪ Variables a, b, and c are all given an integer
value at the same time 1
Session 1
Activity 3
Time for an activity.
!
Hello World Program
▪ On Windows, you can start Jupyter by pressing the shortcut
Anaconda adds to your start menu, which will open a new tab in your
default web browser
▪ Keep in mind that the dashboard will only offer you access to the
files and folders in Jupyter's start-up directory (i.e., where Jupyter or
Anaconda is installed). The start-up directory, on the other hand, can
be altered. On any system, enter the command jupyter notebook at
the command line (or terminal on Unix systems) to start the
dashboard; in this case, the current working directory will be the
start-up directory
▪ You may have noticed that the URL for the dashboard is
https://localhost:8888/tree
▪ When Jupyter Notebook opens in your browser, the term ‘localhost’
does not refer to a website, but rather to the fact that the material is
supplied from your own computer
▪ Jupyter's Notebooks and dashboards are web apps, and Jupyter
creates a local Python server to provide these apps to your web
browser, effectively making it platform-independent and allowing for
simpler online sharing
▪ (Don't worry if you don't get it now; the main idea is that, while
Jupyter Notebooks appears in your browser, it is hosted and
executed on your local PC.) Until you choose to share your
Hello World Program
▪ The dashboard's user interface is fairly
self-explanatory, although we'll return to
it briefly later. So, what do we have to
lose? Navigate to the folder where you
want to save your first notebook, then
select ‘Python 3’ from the ‘New’ drop-
down menu in the top-right corner:
▪ Here we are now! Your first Jupyter
Notebook will open in a new tab; each
notebook has its own tab so you can
have numerous notebooks open at the
same time
▪ When you return to the dashboard, you
should notice the new file Untitled.ipynb
and some green text indicating that your
notebook is operating
Hello World Program
▪ What is an ipynb File?
▪ The short answer is that each .ipynb file
represents a single notebook, thus when you
create a new notebook, a new .ipynb file is
created as well
▪ The longer response is as follows: Each .ipynb
file is a text file that uses the JSON format to
describe the contents of your notebook. Each
cell, as well as its contents, such as picture
attachments that have been converted to text
strings, is listed, along with some metadata
▪ You can edit this yourself—if you know what
you're doing---by going to the notebook's menu
bar and selecting ‘Edit > Edit Notebook
Metadata.’ By selecting ‘Edit’ from the
dashboard controls, you may also see the
contents of your notebook files
▪ However, the term ‘can’ is crucial. In the vast
majority of circumstances, manually editing
your notebook metadata is unnecessary
Hello World Program
▪ The Notebook Interface
▪ Cells
▪ We'll return to kernels later, but now, let's get a handle on cells. A notebook's body is made up of cells. The
box with the green outline in the screenshot of a new notebook in the previous section is an empty cell.
We'll look at two different types of cells:
- A code cell is a block of code that will be executed by the kernel. When the code is run, the output is
displayed underneath the code cell that generated it in the notebook
- When a Markdown cell is run, the output is displayed in-place and the content is formatted using
Markdown
▪ A code cell is always the first cell in a new notebook
▪ Let's put it to the test with a traditional hello world scenario: Type print('Hello World!') into the cell and
press Ctrl + Enter or click the Notebook Run Button in the toolbar above
▪ Input:
▪ The end result should be as follows:
Hello World!
Session 1
Activity 4
Time for an activity.
!
Time for Knowledge Check.
Please go through the Knowledge
Check.