Chapter 4 - Basics of Python
- Authors
- Ms. Samavi Salman
- Dr. Rao Muhammad Adeel Nawab
- Supporting Material
Quick Recap
- Quick Recap – Basics of Programming
In previous Chapter, I presented
- Programming
- Programming is defined as the process of writing Programs
- Program
- A Program Processes the Data according to a Set of Instructions to produce Output
- Programmer
- A Programmer is a person who writes Programs
- Programming Language
- A Programming Language is defined as a formal language comprising of rules to instruct a Computer to perform a specific Set of Operations to produce Output
- Types of Programming Languages
- Programming Languages can be broadly categorized into
- Low-level Programming Languages
- High-level Programming Languages
- Low-level Programming Language
- A Programming Language that corresponds directly to a specific Machine
- Types of Low-level Programming Languages
- Two main types of Low-level Programming Languages are
- Machine Language
- Assembly Language
- Machine Language
- A Computer Programming Language consisting of Binary or Hexadecimal instructions which a Computer can respond to directly
- Mnemonic Code
- A code that can be remembered comparatively easily and that aids its user in recalling the information it represents
- Assembly Language
- A Programming Language that consists of Instructions that are Mnemonic Codes for corresponding Machine Language Instructions
- Two main types of Low-level Programming Languages are
- High-level Programming Languages
- A Programming Language that is independent of the Machine
- Source Code vs Machine Code
- Source Code
- Source Code is defined as a Program written in High Level Programming Language
- Machine Code
- Machine Code is defined as a Program written in Machine Language Instructions that can be executed directly by a Computer
- Source Code
- Interpreter
- An Interpreter converts (translates) a Source Code into Machine Code line-by-line
- Compiler
- Compiler converts (translates) a Source Code into Machine Code all at once
- Assembler
- An Assembler converts (translates) is a Program written in Assembly Language (Source Code) into Machine Code
- Integrated Development Environment (IDE)
- An Integrated Development Environment (IDE) is a Software Application that provides comprehensive facilities to Programmers for Software / Program development
- Programming Languages can be broadly categorized into
A Python Program
- A Python Program
- In Sha Allah, to properly document the Supporting Material, I will call this part of Chapter
- Section 01 – A Python Program
- Python Statement
- Definition
- A Python Statement is a Syntactic Unit of Python Programming Language that expresses some action to be carried out
- Note
- A Python Statement may / may not have Internal Components (for e.g., Expressions)
- Purpose
- The main aim of a Python Statement is to perform a specific action
- Examples – Python Statement
- In Sha Allah, below I will show some Examples of Python Statements
- Example 1 – Python Statement
print("My CGPA is: ", 3.58)
- Example 2 – Python Statement
number = 10
- Example 3 – Python Statement
sum = 4 + 6
- Python Program
- Definition
- A Python Program is a Sequence of one or more Python Statements
- Purpose
- The main aim of a Python Program is to perform a specific Real-world Task
- Nine Popular Software’s (or Applications) Developed in Python Programming Language
- Quora
- Spotify
- Netflix
- Uber
- Dropbox
- Python Control Structures
- Definition
- Python Control Structures are used as a way to specify the Flow of Control in Python Programs
- Purpose
- The main purpose of Python Control Structures is to analyze Python Statements and choose in which direction a Python Program flows based on certain Conditions and / or Parameters
- Importance
- Python Control Structures help us to improve the
- Clarity of a Python Program
- Understandability of a Python Program
- Readability of a Python Program
- Logic of a Python Program
- Python Control Structures help us to improve the
- Python Control Structures
- In Python Programming, three main Python Control Structures are
- Sequential Structure
- Selection Structure
- Repetition Structure
- Sequential Structure
- Definition
- A Sequence Structure executes a Sequence of Python Statements one by one in the Order in which they are written
- Purpose
- The main purpose of Sequential Structure it to
- Execute Python Statements one by one in the Order in which they are written
- The main purpose of Sequential Structure it to
- Programming Languages based on Sequential Structures
- Some of the popular and widely used Programming Languages which execute Programs using Sequential Structure are
- Java
- C
- C++
- Python
- FORTRAN
- Basic
- PL1
- Pascal
- Some of the popular and widely used Programming Languages which execute Programs using Sequential Structure are
- Selection Structure
- Definition
- A Selection Structure (a.k.a. Conditional Structure) is a feature of Python Programming Language that is used to perform different actions / tasks based on whether a specific Condition is True or False
- Purpose
- The main purpose of Selection Structures is
- Decision Making and
- Branchinge., Choosing between two or more alternative paths
- The main purpose of Selection Structures is
- Examples of Selection Structures in Python
- Selection Structures used in Python Programming Language are
- if
- elif
- else
- Selection Structures used in Python Programming Language are
- Note
- For details on Selection Structures
- See Chapter 14 – Decision Making Statements in Python
- For details on Selection Structures
- Repetitive Programming Task
- A Repetitive Programming Task is defined as a Programming Task in which there is little variation from one item to the next item of the Programming Task
- Repetition Structure
- Definition
- A Repetition Structure is a feature of Python Programming Language that is used to perform Repetitive Python Programming Tasks
- Purpose
- The main purpose of Repetition Structure it to perform Repetitive Programming Task
- Repetition Structure in Python
- Repetition Structure of Python Programming Language are
- for loop
- while loop
- Repetition Structure of Python Programming Language are
- Note
- For details on Repetition Structures
- See Chapter 15 – Loops in Python
- For details on Repetition Structures
- Execution of a Python Program
- Question
- How is a Python Program executed?
- Answer
- A Python Program is executed (Python) Statement by (Python) Statement in the Order in which they are writtene., using Sequential Structure
- Note
- Since Python Program is executed using the Sequential Structure
- The Order of Python Statements is important
- Since Python Program is executed using the Sequential Structure
- TIP
- In Life, we also have three Control Structures 😊
- Sequential Structure
- Selection Structure
- Repetition Structure
- Example – A Python Program
- Task Description
- Consider the following Python Program and answer the questions given below?
- Questions
- How many Python Statements are there in the following Python Program?
- How Python Statements will be executed?
print(“Allah has control over everything”)
print(“Allah gives life and He will cause death”)
print(“He is Allah other than whom there is no deity”)
Number of Python Statements |
|
Recall |
|
Execution of Python Statements |
|
Order of Execution of Python Statements |
|
TODO and Your Turn
TODO Task 1
- Task
- Consider the following Tasks and answer the questions given below
- Note
- Your answer should be
- Well Justified
- Your answer should be
Python Program 1 |
print(“حضور صلی اللہ علیہ وسلم نے فرمایا تم میں سے کوئی شخص مومن نہیں ہو سکتا یہاں تک کہ اپنے بھائی کے لیے وہی چیز پسند کرے جو اپنے لیے پسند کرتا ہے“) print(“In 3 situations, if you pray dua, it will definitely be accepted:”) print(“When your ‘body’ starts ‘shaking'”) print(“When you got ‘fear’ in your ‘heart'”) print(“When you have ‘tears’ in your ‘eyes'”) |
Python Program 2 |
number01 = 10 number02 = 7 sum = number01 + number02 difference = number01 – number02 print(“The sum of number01 and number02 is: ”, sum) print(“The difference of number01 and number02 is: ”, difference) |
Python Program 3 |
number01 = 10 square = number01 * number01 print(“The square of number01 is: ”, square) |
- Questions
- How many Python Statements are there in each of the given Python Programs?
- How Python Statements will be executed in each Python Program?
- What will be the Order of Execution of Python Statements in each Python Program?
Your Turn Task 1
- Task
- Find any 10 Python Programs form any Website on the Internet and answer the questions given below
- Note
- Your answer should be
- Well Justified
- Questions
- How many Python Statements are there in each Python Programs?
- How Python Statements will be executed in each Python Program?
- What will be the Order of Execution of Python Statements in each Python Program?
- Your answer should be
Display Data on Output Screen
- Important Note
- In Sha Allah, to properly document the Supporting Material, I will call this part of Chapter
- Section 02 – Display Data on Output Screen
- Data
- Definition
- Raw Facts and Figures
- Examples of Data
- Videos
- Photos
- Audio Files
- E-mail Messages
- Databases
- Names
- Dates
- Addresses
- Credit Card Numbers
- Stock information
- Geo Location etc.
- Types of Data
- The four main Types of Data are
- Text
- Image
- Video
- Audio
- Example 1 – Textual Data
- To become a balanced and characterful personality
- Be Happy, Keep Happy
- Go to bed after Namaz-e-Isha (نماز عشاء)
- Do 30 minutes brisk walk on daily basis
- Do 30 minutes Zikar (ذکر) of Allah (الله) on daily basis
- Offer 5 Namaz on daily basis
- Take 3 meals of balanced diet on daily basis
- Help at least one person (daily) for the sake of Allah (الله)
- Remember
- Small Things have Huge Impact 😊
- Little Efforts Daily Will Make You the Greatest 😊
- Example 1 – Image Data
- Path of Image (used in this Example)
- See jpg File in Section 02 – Display Data on Output Screen of Data Folder
- Example 2 – Image Data
- Path of Image (used in this Example)
- See Allah.png File in Section 02 – Display Data on Output Screen of Data Folder
- Example 1 – Audio Data
- Path of Audio (used in this Example)
- See qasida-burda-shareef.mp3 File in Section 02 – Display Data on Output Screen of Data Folder
- Example 1 – Video Data
- Path of Video (used in this Example)
- See qasida-burda-shareef.mp4 File in Section 02 – Display Data on Output Screen of Data Folder
- Information
- Definition
- Processed form of Data is called Information
- Purpose
- Information helps us to Learn
- Example – Converting Data into Information
- Raw Data
- Sidra, 70, Mehwish, 80, Adeel, 90, Ayesha, 80, Imran, 70
- Converting Data into Information
- Organize Data in some Structured Format (for e.g., Table) to extract meaningful Information
- Information
- The following Table shows Raw Data organized in a Tabular Format (Structured Format)
- Note
- We can extract meaningful Information from the Table given below) including
- Which Student got Highest Marks in Introduction to Python Course?
- Which Student got Lowest Marks in Introduction to Python Course?
- What are the average Marks in Introduction to Python Course?
- We can extract meaningful Information from the Table given below) including
Student ID | Student Name | Marks (Introduction to Python) |
103 | Adeel | 90 |
102 | Mehwish | 80 |
104 | Ayesha | 80 |
101 | Sidra | 70 |
105 | Imran | 70 |
- Displaying Data on Output Screen in Python
- Question
- How do we Display Data (Text, Image, Video and Audio) on the Output Screen?
- Answer
- To Display Data (Text, Image, Video and Audio) on the Output Screen, we mainly use various Functions
- Function
- Definition
- A Function is defined as a Block of Code used to perform a specific Task
- Note
- For details on Functions
- See Chapter 17 – Functions in Python
- For details on Functions
- Types of Functions in Python
- The two main types of Functions in Python are
- Built-in Functions
- User Defined Functions
- Built-in Function
- Definition
- A Built-in Function is defined as a Function which functionality is pre-defined in Python Programming Language
- Examples of Built-in Functions in Python
- Some of the popular and widely used Built-in Functions in Python are as follows
- print()
- Print a Message on Output Screen
- input()
- Take Input from a User
- sqrt()
- Calculate Square Root of a Number
- sum()
- Calculate Sum of Numbers
- min()
- Get a Minimum Value from Multiple Values
- max()
- Get a Maximum Value from Multiple Values
- pow()
- Calculate Power of a Number
- type()
- Return Type (Data Type) of an Object
- User Defined Functions
- Definition
- A User Defined Function is defined as a Function which you can define yourself in Python Programming Language
- Module / Library
- Definition
- In Python Programming Language, a Module / Library is a highest-level Python Program organization unit, which is used to organize Code and Data (for reuse)
- Note
- In Python Programming Language, a Module / Library is simply a Python (Code) File
- Extension of a Python (Code) File is
- .py
- You can use as many Module / Library as you like in a Python Program
- One Module / Library can import another Module / Library
- Extension of a Python (Code) File is
- In Python Programming Language, a Module / Library is simply a Python (Code) File
- Purpose
- The main purpose of Module / Library is to efficiently organize Python Code (and Data) for
- Data and Code Reusability
- The main purpose of Module / Library is to efficiently organize Python Code (and Data) for
- Importance
- Module / Library allow us to use the Code developed by various Expert Programmers for a wide range of Real-world Tasks, without understanding and developing the Code ourselves
- Module / Library reduce time, effort and cost in the development of various Software’s
- Module / Library allow us to use the Code developed by various Expert Programmers for a wide range of Real-world Tasks, without understanding and developing the Code ourselves
- Applications
- The main and biggest advantage of Module / Library is
- Data and Code Organization
- Data and Code Reusability
- The main and biggest advantage of Module / Library is
- Standard Library
- Definition
- A Module / Library that is provided by Python (i.e., you don’t need to rewrite commands) is called Standard Library
- How to Use various Functions of a Module / Library in Python
- Question
- How can we use various Functions of a Module / Library in Python?
- Answer
- To use various Functions of a Module / Library in Python, you need to follow a Three Step Process
- Step 1: Install Module / Library
- Step 2: Import Entire Module / Library (or Import Specific Function(s) of a Module / Library)
- Step 3: Use Functions of the Module / Library
- To use various Functions of a Module / Library in Python, you need to follow a Three Step Process
- Note
- You can
- Import an entire Module / Library OR
- Import specific Function(s) of a Module / Library
- You can
- Important Note
- You must install a Module / Library before using it in your Code 😊
- How to Install a Python Module / Library
- Question
- How can we install a Python Module / Library?
- Answer
- Use pip install Command
- Example 1 - How to Install a Python Module / Library
- Task Description
- Install ipython Module / Library
- Note
- In Python Programming Language, ipython is a toolkit which contains many tools for interactive and parallel computing in Python
- Approach One – Installation of ipython Module / Library
- Step 1: Open a Jupyter Notebook File
- Step 2: In any Cell of your Jupyter Notebook File: (1) write the following Command and, (2) press Shift + Enter key or Run the command from the button given in Jupyter Notebook (as shown in the Figure below)
pip install ipython
- Step 3: When ipython Module is successfully installed a Message will be displayed on Jupyter Notebook (as shown in Figure above)
- Step 4: After Installation, verify the Installation of ipython Module using the following Command
IPython.version_info
OR
IPython.__version__
- Approach Two – Installation of ipython Module
- Step 1: Open a Terminal / CMD (Command Line Prompt)
- Step 2: On the Terminal / CMD, write the following Command and press Enter key (as shown in the Figure below)
pip install ipython
- Step 3: When ipython Module / Library is successfully installed a Message will be displayed on Terminal / CMD (as shown in the Figure below)
- Step 4: After Installation, verify the Installation of ipython Module by typing the following Command in Terminal / CMD and pressing the Enter key
Ipython
- Step 5: To check the version of ipython Module, type the following Command on Terminal / CMD and Press Enter key
IPython.version_info
OR
IPython.__version__
- Example 2 - How to Install a Python Module / Library
- Task Description
- Install Matplotlib Module / Library
- Note
- In Python Programming Language, Matplotlib is a plotting Library
- Approach One – Installation of Matplotlib Module / Library
- Step 1: Open a Jupyter Notebook File
- Step 2: In any Cell of your Jupyter Notebook File: (1) write the following Command and, (2) press Shift + Enter key or Run the command from the button given in Jupyter Notebook (as shown in the Figure below)
pip install Matplotlib
- Step 3: When Matplotlib Module / Library is successfully installed a Message will be displayed on your Jupyter Notebook
- Approach Two – Installation of matplotlib Module / Library
- Step 1: Open a Terminal / CMD (Command Line Prompt)
- Step 2: On the Terminal / CMD, write the following Command and press Enter key
pip install Matplotlib
- Step 3: When Matplotlib Module / Library is successfully installed a Message will be displayed on Terminal / CMS
- Step 5: To check the version of ipython Module, type the following Command on Terminal / CMD and Press Enter key
- Example 1 - How to Uninstall a Python Module / Library
- Task Description
- Uninstall ipython Module / Library
- Approach One – Uninstalling ipython Module / Library
- Step 1: Open a Jupyter Notebook File
- Step 2: In any Cell of your Jupyter Notebook File: (1) write any of the two Comments given below and, (2) press Shift + Enter key
- Note that if the Program Control moves to the Next Cell in Jupyter Notebook File, then ipython Module / Library is successfully uninstalled
pip uninstall ipython –y
- Note:
- In Jupyter Notebook, it is mandatory to write –y with pip uninstall ipython Command
- If you don’t write -y with pip uninstall ipython Command, then Jupyter Notebook will be stuck and ipython will not be uninstalled (see Figure below)
- Approach Two – Uninstalling ipython Module / Library
- Step 1: Open a Terminal / CMD (Command Line Prompt)
- Step 2: On the Terminal / CMD: (1) write the following Command and, (2) press Enter key (as shown in Figure below)
pip uninstall ipython
- Step 3: Verify the uninstallation of ipython Module by: (1) writing the following Command on Terminal / CMD and, (2) pressing Enter key
Ipython
- Important Note
- For Installation, we will use the Term
- Ipython
- For Import, we will use the Term
- ipython
- The Figure below shows the difference in two Terms e., Ipython and ipython
- Examples – Importing an Entire Module / Library
- Example 1 – Task Description
- Write Code to import math Module / Library
import math
- Example 2 – Task Description
- Write Code to import sys Module / Library
import sys
- Example 3 – Task Description
- Write Code to import numpy Module / Library
import numpy
- Example 4 – Task Description
- Write Code to import pandas Module / Library
import pandas
- Example 5 – Task Description
- Write Code to import torch Module / Library
import torch
- Importing Specific Function(s) from a Module / Library
- Question
- How can we Import Specific Function(s) from a Module / Library?
- Answer
- The Syntax to import Specific Function(s) from a Module / Library is
from MODULE_NAME import FUNCTION_NAME
- Examples – Importing Specific Function(s) from a Module / Library
- Example 1 – Task Description
- Write Code to import sqrt() and factorial() Functions form the math Module / Library
from math import sqrt, factorial
- Example 2 – Task Description
- Write Code to import randint() Function form the random Module / Library
from random import randint
- Example 3 – Task Description
- Write Code to import dataframe() Function form the pandas Module / Library
from pandas import dataframe
- Example 4 – Task Description
- Write Code to import array() Function form the numpy Module / Library
from numpy import array
- Example 5 – Task Description
- Write Code to import pyplot() Function form the matplotlib Module / Library
from matplotlib import pyplot
- Example – Importing an Entire Module
- Path of Image (used in this Example)
- See Durood-Sharif-Card.jpg File in Chapter – 01\Data\Section 02 – Display Data on Output Screen
Completely and Correctly Understand the Task |
Task Description |
|
Required Output |
Plan and Design Solution of the Task |
Input-Processing-Output |
|
Implementation in Python |
Code |
'''
__author__ = Ms. Samavi Salman
__copyright__= Copyright (C) 2020 Ms. Samavi Salman
__license__ = Public Domain
__version__ = 1.0
'''
# Import Module
import IPython
# Input + Processing + Output
# Display Image on the Output Screen
IPython.display.Image(filename="–..\Data\Section 02 - Display Data on Output Screen\Durood-Sharif-Card.jpg", width=700, height=100)
Output of Code |
Analysis, Main Findings and Conclusions |
Analysis of Python Program |
|
Main Findings |
|
Conclusions |
|
- Example – Importing an Entire Module
- Path of Image (used in this Example)
- See Durood-Sharif-Card.jpg File in Chapter – 01\Data\Section 02 – Display Data on Output Screen
Completely and Correctly Understand the Task |
Task Description |
|
Required Output |
Plan and Design Solution of the Task |
Input-Processing-Output |
|
Implementation in Python |
Code |
'''
__author__ = Ms. Samavi Salman
__copyright__= Copyright (C) 2020 Ms. Samavi Salman
__license__ = Public Domain
__version__ = 1.0
'''
# Import Image() Function of IPython Module
from IPython.display import Image
# Input + Processing + Output
# Display Image (Data) on the Output Screen
Image(filename="–..\Data\Section 02 - Display Data on Output Screen\Durood-Sharif-Card.jpg", width=700, height=100)
Output of Code |
Analysis, Main Findings and Conclusions |
Analysis of Python Program |
|
Main Findings |
|
Conclusions |
|
- Most Popular and Widely Used Modules in Python
- Some of the most popular and widely used Modules in Python are
- NumPy
- Requests
- Pandas
- TensorFlow
- Scikit-Learn
- Keras
- PyTorch
- SciPy
- Note
- In Sha Allah, in this Section, I will show how we can use various Functions in Python to Display Data (Text, Image, Audio and Video) on the Output Screen
Displaying Textual Data on Output Screen
- Important Note
- In Sha Allah, to properly document the Supporting Material, I will call this part of Chapter
- Section 2.1 – Displaying Textual Data on Output Screen
- Object in Real-world
- Definition
- In Real-world, an Object is a material thing that can be seen and touched
- Example 1 – Object in Real-world
- Student is an Object
- Characteristics
- Student ID
- Name
- Gender
- Degree Program
- CGPA
- Behavior
- Sleep
- Study
- Eat
- Run / Walk
- Characteristics
- Example 2 – Object in Real-world
- Car is an Object
- Characteristics
- Name
- Color
- Model
- Price
- Fuel Efficiency
- Behavior
- Turning
- Braking
- Accelerating
- Characteristics
- Real-world Object Representation in Object Oriented Programming (OOP)
- In an Object-Oriented Programming (OOP) Language
- Characteristics of a Real-world Object is
- Represented using Variables
- Behavior of a Real-world Object is
- Represented using Functions / Methods
- Note
- Python is an Object-Oriented Programming (OOP) Language
- Characteristics of a Real-world Object is
- Object in Python
- Since Python is an Object-Oriented Programming Language
- Therefore, almost everything in Python is an Object
- Representation of Characteristics of an Object
- In Python, Characteristics of a Real-world Object is represented with
- Variables
- Representation of Behavior of an Object
- In Python, Behavior of a Real-world Object is represented with
- Functions / Methods
- In Python, Behavior of a Real-world Object is represented with
- In Python, Characteristics of a Real-world Object is represented with
- Classification of Objects in Python
- In Python, an Object can be classified as either
- Mutable (Changeable)
- Immutable (Unchangeable)
- Examples – Mutable Objects
- List
- Set
- Dictionary
- Examples – Immutable Objects
- Int
- Float
- Boolean
- String
- Tuple
- Displaying Textual Data on the Output Screen
- Question
- How can we Display Textual Data on the Output Screen?
- Answer
- Use print() Built-in Function in Python
- print() Function
- Definition
- The print() Function is a Built-in Function of Python Programming Language and used to Display a Python Object (mainly Text) on the Output Screen
- Purpose
- The main purpose of the print() Function it to print / display
- Messages (Textual Data) on the Output Screen
- The main purpose of the print() Function it to print / display
- Importance
- The print() Function is one of the most popular and widely used Built-in Functions in Python Programming Language
- Applications
- A print() Function helps us to display / print
- Different useful Messages (Text) on the Output Screen
- Values of different Types of Constants (Integer, Float, Character and String) on the Output Screen
- Values of different Types of Variables (Integer, Float, Character and String) on the Output Screen
- Output Values of different Types of Expressions on the Output Screen
- A print() Function helps us to display / print
- Suitable to Use
- The print() Function is suitable to use when we want to
- Display some useful and important Message and / or Data for the End User
- Take Input from the End User
- Display Results (or Output) to the End User
- Display Exceptions (Errors) on the Output Screen
- The print() Function is suitable to use when we want to
- Syntax – print() Function
- The Syntax of print() Function is as follows
print(object(s), sep = separator, end = end)
- Recall
- The main purpose of the print() Function is to display / print a
- a specified Message to the Output Screen
- The main purpose of the print() Function is to display / print a
- In above Syntax, object(s) refers to
- Message (to be displayed / printed on the Output Screen), which can be a
- String or
- any other object
- Message (to be displayed / printed on the Output Screen), which can be a
- Important Note
- In print() Function, object(s) will be converted into a
- String before displaying / printing on the Output Screen
- In print() Function, object(s) will be converted into a
- Parameters – print() Function
Parameter Name |
Description |
Default Value |
Status |
object(s) | One or more objects(s). Note that all objects will be converted to String before displaying them on the Output Screen | Mandatory | |
end = ‘end’ | Use to specify what to print at the end of the object(s) that are converted to String before displaying on the Output Screen | \n | Optional |
sep | Use to specify how to separate the objects, if there are more than one object. | ‘ ‘ | Optional |
- Important Note
- By default, in a print() Python Statement, the last Character is \n (which moves the Program Control to the Next Line)
- Print Message(s) on Output Screen using print() Function
- In Sha Allah, in the next Slides, I will show how we can print / display Message(s) on the Output Screen using print() Function in various languages (Arabic, Urdu and English)
- Example 1 – Displaying Textual Data on Output Screen
Completely and Correctly Understand the Task |
Task Description |
|
Required Output |
Creator of Heaven and Earth is: Allah |
Plan and Design Solution to the Task |
Input-Processing-Output |
|
Implementation in Python |
Code |
'''
__author__ = Ms. Samavi Salman
__copyright__= Copyright (C) 2020 Ms. Samavi Salman
__license__ = Public Domain
__version__ = 1.0
'''
# Input + Processing + Output
print("Creator of Heaven and Earth is: Allah")
Creator of Heaven and Earth is: Allah
Analysis, Main Findings and Conclusions |
Analysis of Python Program |
|
Findings |
|
Conclusion |
|
- Example 2 – Displaying Textual Data on Output Screen
Completely and Correctly Understand the Task |
Task Description |
|
Required Output |
The Last Messenger of Allah and Role Model for all of us in all Walks of Life is: Hazrat Muhammad S.A.W.W. |
Plan and Design Solution to the Task |
Input-Processing-Output |
|
Implementation in Python |
Code |
'''
__author__ = Ms. Samavi Salman
__copyright__= Copyright (C) 2020 Ms. Samavi Salman
__license__ = Public Domain
__version__ = 1.0
'''
# Input + Processing + Output
print("The Last Messenger of Allah and Role Model for all of us in all Walks of Life is: Hazrat Muhammad S.A.W.W.")
The Last Messenger of Allah and Role Model for all of us in all Walks of Life is: Hazrat Muhammad S.A.W.W.
Analysis, Main Findings and Conclusions |
Analysis of Python Program |
|
Findings |
|
Conclusion |
|
- Example 3 – Displaying Textual Data on Output Screen
Completely and Correctly Understand the Task |
Task Description |
|
Required Output |
Hazrat Abdullah and Hazrat Amna |
Plan and Design Solution to the Task |
Input-Processing-Output |
|
Implementation in Python |
Code |
'''
__author__ = Ms. Samavi Salman
__copyright__= Copyright (C) 2020 Ms. Samavi Salman
__license__ = Public Domain
__version__ = 1.0
'''
# Input + Processing + Output
print("Hazrat Abdullah and Hazrat Amna")
Hazrat Abdullah and Hazrat Amna
Analysis, Main Findings and Conclusions |
Analysis of Python Program |
|
Findings |
|
Conclusion |
|
Escape Sequences
- Important Note
- In Sha Allah, to properly document the Supporting Material, I will call this part of Chapter
- Section 03 – Escape Sequences
- Recap – print() Function
- In previous Section, we used the print() Function to display Text (Sequence of Characters) on the Output Screen
- Question
- How can we add Special Characters and handle complex Formatting using print() Function?
- Answer
- Use Escape Sequences
- Escape Sequences
- Definition
- An Escape Sequence is defined using a combination of Two Characters, where
- First Character is a Backslash \ and
- Second Character is a Letter
- An Escape Sequence is defined using a combination of Two Characters, where
- Purpose
- The main purpose of the Escape Sequences is to
- add Special Characters and
- handle complex Formatting
- The main purpose of the Escape Sequences is to
- Importance
- Without Escape Sequences it will not be possible to properly Display Data (Text, Image, Audio and Video) on the Output Screen
- Applications
- Escape Sequences help us to
- Display Data (Text, Image, Audio and Video) with high quality Formatting, on the Output Screen, which increases the understandability of a Program
- Display Complex Data (for e.g., Equations) on the Output Screen
- Escape Sequences help us to
- Main Escape Sequences in Python
- In Python Code, Character \ and Character(s) following it are replaced with
- A Single Character
- Some of the popular and widely used Escape Sequences of Python Programming Language are as follows
- New Line (\n)
- In Python Code, Two Characters \n is replaced with
- Newline Character
- In Python Code, Two Characters \n is replaced with
- Tab (\t)
- In Python Code, Two Characters \t is replaced with
- Tab Character
- In Python Code, Two Characters \t is replaced with
- Carriage Return (\r)
- In Python Code, Two Characters \r is replaced with
- Resets a Cursor to the Beginning of a Line of Text
- In Python Code, Two Characters \r is replaced with
- Single Quote (\‘)
- In Python Code, Two Characters \’ is replaced with
- ‘
- In Python Code, Two Characters \’ is replaced with
- Double Quotes (\“)
- In Python Code, Two Characters \” is replaced with
- “
- In Python Code, Two Characters \” is replaced with
- Backslash (\\)
- In Python Code, Two Characters \\ is replaced with
- \
- In Python Code, Two Characters \\ is replaced with
- Triple Quotes (\“””)
- In Python Code, Two Characters \“”” is replaced with
- “””
- In Python Code, Two Characters \“”” is replaced with
- New Line (\n)
- Note
- In Sha Allah, in the next Section, I will explain the working of the following Escape Sequences with Examples
Using \n Escape Sequence in print() Function
- Note
- In Sha Allah, to properly document the Supporting Material, I will call this part of Chapter
- Section 3.1 – Using \n Escape Sequence in print() Function
- Example 1 - Using \n Escape Sequence in print() Function
Completely and Correctly Understand the Task |
Task Description |
|
Required Output |
Allah is One. Allah created us. Allah has control over everything. |
Plan and Design Solution to the Task |
Input-Processing-Output |
|
Implementation in Python |
Code |
'''
__author__ = Ms. Samavi Salman
__copyright__= Copyright (C) 2020 Ms. Samavi Salman
__license__ = Public Domain
__version__ = 1.0
'''
# Input + Processing + Output
# \n Escape Sequence moves Cursor to the Newline
print("Allah is One.\nAllah created us.\nAllah has control over everything.")
Allah is One.
Allah created us.
Allah has control over everything.
Analysis, Main Findings and Conclusions |
Analysis of Python Program |
|
Findings |
|
Conclusion |
|
- Example 2 - Using \n Escape Sequence in print() Function
Completely and Correctly Understand the Task |
Task Description |
|
Required Output |
Allah is One.
Allah created us.
Allah has control over everything. |
Plan and Design Solution to the Task |
Input-Processing-Output |
|
Implementation in Python |
Code |
'''
__author__ = Ms. Samavi Salman
__copyright__= Copyright (C) 2020 Ms. Samavi Salman
__license__ = Public Domain
__version__ = 1.0
'''
# Input + Processing + Output
# \n Escape Sequence moves Cursor to the Newline
print("Allah is One.\n\nAllah created us.\n\nAllah has control over everything.")
Allah is One.
Allah created us.
Allah has control over everything.
Analysis, Main Findings and Conclusions |
Analysis of Python Program |
|
Findings |
|
Conclusion |
|
- Example 3 - Using \n Escape Sequence in print() Function
Completely and Correctly Understand the Task |
Task Description |
|
Required Output |
Allah is One.
Allah created us.
Allah has control over everything. |
Plan and Design Solution to the Task |
Input-Processing-Output |
|
Implementation in Python |
Code |
'''
__author__ = Ms. Samavi Salman
__copyright__= Copyright (C) 2020 Ms. Samavi Salman
__license__ = Public Domain
__version__ = 1.0
'''
# Input + Processing + Output
# \n Escape Sequence moves Cursor to the Newline
print("Allah is One.\n\n\nAllah created us.\n\n\nAllah has control over everything.")
Allah is One.
Allah created us.
Allah has control over everything.
Analysis, Main Findings and Conclusions |
Analysis of Python Program |
|
Findings |
|
Conclusion |
|
Using \t Escape Sequence in print() Function
- Important Note
- In Sha Allah, to properly document the Supporting Material, I will call this part of Chapter
- Section 3.2 – Using \t Escape Sequence in print() Function
- Example 1 – Using \t Escape Sequence in print() Function
Completely and Correctly Understand the Task |
Task Description |
|
Required Output |
Allah is One. Allah created us. Allah has control over everything. |
Plan and Design Solution to the Task |
Input-Processing-Output |
|
Implementation in Python |
Code |
'''
__author__ = Ms. Samavi Salman
__copyright__= Copyright (C) 2020 Ms. Samavi Salman
__license__ = Public Domain
__version__ = 1.0
'''
# Input + Processing + Output
# \t Escape Sequence adds 4 spaces before the Text
# By default 1 Tab = 4 Spaces
print("Allah is One.\tAllah created us.\tAllah has control over everything.")
Allah is One. Allah created us. Allah has control over everything.
Analysis, Main Findings and Conclusions |
Analysis of Python Program |
|
Findings |
|
Conclusion |
|
- Example 2 – Using \t Escape Sequence in print() Function
Completely and Correctly Understand the Task |
Task Description |
|
Required Output |
Hazrat Muhammad S.A.W.W. is the last Messenger of Allah. He is role model for us in all walks of life. May Allah bless us with love of Hazrat Muhammad S.AW.W. Ameen |
Plan and Design Solution to the Task |
Input-Processing-Output |
|
Implementation in Python |
Code |
'''
__author__ = Ms. Samavi Salman
__copyright__= Copyright (C) 2020 Ms. Samavi Salman
__license__ = Public Domain
__version__ = 1.0
'''
# Input + Processing + Output
# \t Escape Sequence adds 4 spaces before the Text
# By default 1 Tab = 4 Spaces
print("Hazrat Muhammad S.A.W.W. is the last Messenger of Allah. \tHe is role model for us in all walks of life.\tMay Allah bless us with love of Hazrat Muhammad S.AW.W.\tAmeen")
Hazrat Muhammad S.A.W.W. is last Messenger of Allah. He is role model for us in all walks of life. May Allah us bless us with love of Hazrat Muhammad S.AW.W. Ameen
Analysis, Main Findings and Conclusions |
Analysis of Python Program |
|
Findings |
|
Conclusion |
|
Using \’, \”, \”””, \n and \t Escape Sequence in print() Function
- Important Note
- In Sha Allah, to properly document the Supporting Material, I will call this part of Chapter
- Section 3.3 – Using \’, \”, \”””, \n and \t Escape Sequence in print() Function
- Example 1 - Using \’, \”, \”””, \n and \t Escape Sequence in print() Function
Completely and Correctly Understand the Task |
Task Description |
|
Required Output |
“Allah is One.” ‘Allah created us.’ ”’Allah has control over everything.”’ ”’Allah loves those who love the humanity for Raza of Allah.”’ |
Plan and Design Solution to the Task |
Input-Processing-Output |
|
Implementation in Python |
Code |
'''
__author__ = Ms. Samavi Salman
__copyright__= Copyright (C) 2020 Ms. Samavi Salman
__license__ = Public Domain
__version__ = 1.0
'''
# Input + Processing + Output
print('"Allah is One."' "\t" "'Allah created us.'" "\n" "'''Allah has control over everything.'''" "\n" "'''"'''Allah loves those who love the humanity for Raza of Allah.'''"'''")
"Allah is One." 'Allah created us.'
'''Allah has control over everything.'''
'''Allah loves those who love the humanity for Raza of Allah.'''
Analysis, Main Findings and Conclusions |
Analysis of Python Program |
|
Findings |
|
Conclusion |
|
- Example 2 - Using \’, \”, \”””, \n and \t Escape Sequence in print() Function
Completely and Correctly Understand the Task |
Task Description |
|
Required Output |
“Hazrat Muhammad S.A.W.W. is the last Messenger of Allah. “ ‘He’s role model for us in all walks of life.’ ”’May Allah bless us with love of Hazrat Muhammad S.AW.W.”’ ”’Ameen”’ |
Plan and Design Solution to the Task |
Input-Processing-Output |
|
Implementation in Python |
Code |
'''
__author__ = Ms. Samavi Salman
__copyright__= Copyright (C) 2020 Ms. Samavi Salman
__license__ = Public Domain
__version__ = 1.0
'''
# Input + Processing + Output
print('"Hazrat Muhammad S.A.W.W. is the last Messenger of Allah. "' "\n" "'He’s role model for us in all walks of life.'" "\t" "'''May Allah bless us with love of Hazrat Muhammad S.AW.W.'''" "\t" "'''"'''Ameen'''"'''")
"Hazrat Muhammad S.A.W.W. is the last Messenger of Allah. "
'He’s role model for us in all walks of life.' '''May Allah bless us with love of Hazrat Muhammad S.AW.W.''' '''Ameen'''
Analysis, Main Findings and Conclusions |
Analysis of Python Program |
|
Findings |
|
Conclusion |
|
TODO and Your Turn
TODO Task 1
- Task
- Consider the following Tasks and answer the questions given below
- Note
- Your answer should be
- Well Justified
- Your answer should be
- Questions
- Write Python Programs for all the Tasks given above by following the Template given in this Chapter i.e.
- Completely and Correctly Understand the Task
- Task Description
- Required Output
- Plan and Design Solution of the Task
- Input-Processing-Output
- Input
- Data
- Instructions
- Output
- Processing
- Input
- Input-Processing-Output
- Implementation in Python
- Code
- Output of Code
- Analysis, Main Findings and Conclusions
- Analysis of Python Program
- Main Findings
- Conclusions
- Completely and Correctly Understand the Task
- Write Python Programs for all the Tasks given above by following the Template given in this Chapter i.e.
Task 1 |
Completely and Correctly Understand the Task |
Task Description |
|
Required Output |
حضرت عبداللہ بن عمرو بن عاص رضی اللہ عنہ سے سنا ، وہ فرما رہے تھے : ایک آدمی نے رسول اللہﷺ سے پوچھا مسلمانوں میں سے بہتر کون ہے ؟ آپ نے فرمایا : ’’ جس کی زبان اور ہاتھ سے دیگر مسلمان امن میں ہوں |
Your Turn Task 1
- TasK
- Write at least 10 different Tasks which are very similar to the ones given in the TODO Tasks
- For each Task write a Python Program using the following Template mentioned in this Chapter i.e.,
- Completely and Correctly Understand the Task
- Task Description
- Required Output
- Plan and Design Solution of the Task
- Input-Processing-Output
- Input
- Data
- Instructions
- Output
- Processing
- Input
- Input-Processing-Output
- Implementation in Python
- Code
- Output of Code
- Analysis, Main Findings and Conclusions
- Analysis of Python Program
- Main Findings
- Conclusions
- Completely and Correctly Understand the Task
Chapter Summary
- Chapter Summary
In this Chapter, I presented the following main concepts:
- Python Statement
- A Python Statement is a Syntactic Unit of Python Programming Language that expresses some action to be carried out
- Python Program
- A Python Program is a Sequence of one or more Python Statements
- Python Control Structures
- Python Control Structures are used as a way to specify the Flow of Control in Python Programs
- In Python Programming, three main Python Control Structures are
- Sequential Structure
- Selection Structure
- Repetition Structure
- Sequential Structure
- A Sequence Structure executes a Sequence of Python Statements one by one in the Order in which they are written
- Selection Structure
- A Selection Structure (a.k.a. Conditional Structure) is a feature of Python Programming Language that is used to perform different actions / tasks based on whether a specific Condition is True or False
- Repetition Structure
- A Repetition Structure is a feature of Python Programming Language that is used to perform Repetitive Python Programming Tasks
- Data
- Raw Facts and Figures
- Information
- Processed form of Data is called Information
- Function
- A Function is defined as a Block of Code used to perform a specific Task
- Built-in Function
- A Built-in Function is defined as a Function which functionality is pre-defined in Python Programming Language
- User Defined Functions
- A User Defined Function is defined as a Function which you can define yourself in Python Programming Language
- Module / Library
- In Python Programming Language, a Module / Library is a highest-level Python Program organization unit, which is used to organize Code and Data (for reuse)
- Standard Library
- A Module / Library that is provided by Python (i.e., you don’t need to rewrite commands) is called Standard Library
- Object in Real-world
- In Real-world, an Object is a material thing that can be seen and touched
- print() Function
- The print() Function is a Built-in Function of Python Programming Language and used to Display a Python Object (mainly Text) on the Output Screen
- Escape Sequences
- An Escape Sequence is defined using a combination of Two Characters, where
- First Character is a Backslash \ and
- Second Character is a Letter
- Some of the popular and widely used Escape Sequences of Python Programming Language are as follows
- New Line (\n)
- In Python Code, Two Characters \n is replaced with
- Newline Character
- Tab (\t)
- In Python Code, Two Characters \t is replaced with
- Tab Character
- In Python Code, Two Characters \t is replaced with
- Carriage Return (\r)
- In Python Code, Two Characters \r is replaced with
- Resets a Cursor to the Beginning of a Line of Text
- In Python Code, Two Characters \r is replaced with
- Single Quote (\‘)
- In Python Code, Two Characters \’ is replaced with
- ‘
- In Python Code, Two Characters \’ is replaced with
- Double Quotes (\“)
- In Python Code, Two Characters \” is replaced with
- “
- In Python Code, Two Characters \” is replaced with
- Backslash (\\)
- In Python Code, Two Characters \\ is replaced with
- \
- In Python Code, Two Characters \\ is replaced with
- Triple Quotes (\“””)
- In Python Code, Two Characters \“”” is replaced with
- “””
- In Python Code, Two Characters \“”” is replaced with
- In Python Code, Two Characters \n is replaced with
- New Line (\n)
- An Escape Sequence is defined using a combination of Two Characters, where
In Next Chapter
- In Next Chapter
- In Sha Allah, in the next Chapter, I will present a detailed discussion on
- Constants in Python
- Comparing Example 1 vs Example 2 vs Example 3
Example 1 – Expression is 7 + 10 |
|
Example 2 – Expression is number1 + 5 |
|
Example 3 – Expression is number1 + number2 |
|
Analysis |
|
Conclusion |
|
- Comparing Example 1 vs Example 2 vs Example 3
Example 1 – Expression is 7 + 10 |
|
Example 2 – Expression is number1 + 5 |
|
Example 3 – Expression is number1 + number2 |
|
Analysis |
|
Conclusion |
|