Skip to content

Ilm o Irfan

Technologies

  • Home
  • Courses
    • Free Courses
  • Motivational Seminars
  • Blog
  • Books
  • Contact
Menu
  • Home
  • Courses
    • Free Courses
  • Motivational Seminars
  • Blog
  • Books
  • Contact
Search
Close this search box.
Explore Courses

Introduction to Python

  • August 29, 2022
  • No Comments
  • Home
  • Free Book

Table of Contents

Chapter 6 - Variables in Python

  • Authors
  • Ms. Samavi Salman
  • Dr. Rao Muhammad Adeel Nawab
  • Supporting Material
  • Download Supporting Material (Code): here

Quick Recap

  • Quick Recap – Constants in Python

In previous Chapter, I presented the following main concepts:

  • Constants
    • Constants are defined as those (Data) Values that do not change (i.e., remain the same) every time a Python Program is executed
  • The two main types of Numeric Constants are
    • Integer Constants
    • Float Constants
  • Integer Constants
    • Integer Constants are defined as those (Integer) Values that do not change (i.e., remain the same) every time a Python Program is executed
  • Float Constants
    • Float Constants are defined as those (Float) Values that do not change (i.e., remain the same) every time a Python Program is executed
  • String
    • A String is defined as a Sequence of Characters
  • String Constants
    • String Constants are defined as those Fixed Sequence of Characters that do not change (i.e., remain the same) every time a Python Program is executed
  • The two main types of String Constants are
    • String Constant Comprising of a Single Character
    • String Constant Comprising of Multiple Characters

Variable

  • Variables
  • Definition
    • A Variable is defined as the Name Assigned to a Memory Location / Memory Address (to store Data (Values / Constants)), that can change (i.e., does not remain the same) every time a Python Program is executed
  • Purpose
    • The main purpose of a Variable is to
      • Store Data (Values / Constants) that can be used for further processing
  • Importance
    • Variables help us to
      • Efficiently store and use various types of Real-world Data in our Python Programs
      • Write high-quality Code (Programs)
  • Applications
    • Variables are used in almost each and every Python Program 😊
  • Note
    • In Python, Variables are mutable i.e.
      • Value of a Variable can be changed
      • Variable can be deleted
    • In Python, Variables don’t need explicit Declaration to occupy Memory Space
  • Variable vs Value of a Variable
  • Note the difference between these two Concepts
    • Variable
    • Value of a Variable
  • Value of a Variable – Definition
    • Value of a Variable is defined as the Content (Value / Constant) stored in a Variable
  • Three Main Components of a Variable
  • Three main Components of a Variable are
    • Variable Name
    • Value (stored) in a Variable (i.e., Content of a Variable)
    • Memory Location / Memory Address (used to store Value of a Variable)
  • Main Operations on a Variable
  • The main Operations performed on a Variable are
    • Operation 01
      • Store Value in a Variable
    • Operation 02
      • Modify / Update Value of a Variable
    • Operation 03
    • Use Value of a Variable for various Tasks
  • Note
    • All Operations are performed on the Value (of a Variable)
    • To perform all above mentioned Operations on a Variable, we will use
      • Variable Name
    • To perform Operation 02 and / or Operation 03, you must
      • First perform Operation 01
  • Types of Variables
  • In Python, there are three main Types of Variables
    • Integer Variables
    • Float Variables
    • String Variables
  • Integer Variable
  • Definition
    • An Integer Variable is defined as the Name Assigned to a Memory Location / Memory Address (to store Integer Values (Constants)), that can change (i.e., does not remain the same) every time a Python Program is executed
  • Purpose
    • The main purpose of an Integer Variable is to
      • Store Data (Integer Value / Integer Constant) that can be used for further processing
  • Float Variable
  • Definition
    • A Float Variable is defined as the Name Assigned to a Memory Location / Memory Address (to store Floating-Point Values (Constants)), that can change (i.e., does not remain the same) every time a Python Program is executed
  • Purpose
    • The main purpose of a Float Variable is to
      • Store Data (Floating-Point Value / Floating-Point Constant) that can be used in further processing
  • String Variable
  • Definition
    • A String Variable is defined as the Name Assigned to a Memory Location / Memory Address (to store Fixed Sequence of Characters (Constants)), that can change (i.e., does not remain the same) every time a Python Program is executed
  • Purpose
    • The main purpose of a String Variable is to
      • Store Data (String Constant) that can be used for further processing
  • Main purpose of a Variable
  • Question
    • What is the main purpose of a Variable?
  • Answer
    • To store a Value and
      • Use it (i.e., Value of the Variable) for different Operations to accomplish a given Task
  • Assignment Operator (=)
  • Question
    • Which Operator is used to perform the following Two Operations on a Variable?
      • Operation 01
        • Store Value in a Variable
      • Operation 02
        • Modify / Update Value of a Variable
      • Answer
        • Assignment Operator (=)

Store Value in a Variable

  • Storing Value in a Variable
  • Question
    • How can we store Value in a Variable?
  • Answer
    • Use Assignment Statement
  • Approaches to Store Value in a Variable
  • Question
    • What are the main approaches to store Value in a Variable?
  • Answer
    • We can store Value in a Variable using two main approaches
      • Approach 01
        • Store Value in a Variable at Coding Time
      • Approach 02
        • Store Value in a Variable at Run Time
  • Important Note
  • You must Declare a Variable before storing Value in it
  • The Data Type of Variable must match with the Value (Constant) stored in it i.e.
    • Integer Variable can store Integer Value (Constant)
    • Float Variable can store Float Value (Constant)
    • String Variable can store String Value (Constant)
  • Syntax - Assignment Statement
  • The Assignment Statement comprises of three Parts
    • Part 01: Left Hand Side (LHS) of Assignment Operator (=)
    • Part 02: Assignment Operator (=)
    • Part 03: Right Hand Side (RHS) of Assignment Operator (=)
  • Syntax – Assignment Statement
				
					Left Hand Side (LHS) = Right Hand Side (RHS)
				
			
  • Note
    • On LHS (of Assignment Operator (=)), we can only have a
      • Variable
    • On RHS (of Assignment Operator (=)), we may have a
      • Constant / Variable / Expression / Function
    • Note
      • For details on Expressions
        • See Chapter 7 – Operators and Expressions
      • For details on Functions
        • See Chapter 17 – Functions in Python
  • Syntax - Assignment Statement Cont.…
				
					Left Hand Side (LHS) = Right Hand Side (RHS)
Variable = Constant / Variable / Expression / Function
				
			

Storing Value in a Variable at Code Time using Assignment Statement

  • Store Value in a Variable at Code Time using Assignment Statement
  • Question
    • How can we store Value in a Variable at Code Time using Assignment Statement?
  • Answer
    • To store Value in a Variable at Code Time using Assignment Statement, follow the following Two Step Process
      • Step 1: Declare a Variable
      • Step 2: Store Value in the Variable declared in the Step 1 (using Assignment Statement)
    • Note
      • In Python Programming Language, the above two Steps are accomplished in
        • A Single Python Statement
  • Syntax – Declaring and Storing Value in a Variable at Code Time using Assignment Statement
				
					variable_name = value_to_be_stored_in_variable
				
			
  • Example 1 - Declaring and Storing Value in an Integer Variable at Code Time using Assignment Statement
				
					'''
Declaring an Integer Variable and Storing Integer Value(Constant) in it
'''

number = 10

				
			
  • Example 2 - Declaring and Storing Value in a Float Variable at Code Time using Assignment Statement
				
					'''
Declaring a Float Variable and Storing Float Value (Constant) in it
'''

number = 25.44

				
			
  • Example 3 - Declaring and Storing Value in a String Variable at Code Time using Assignment Statement
				
					'''
Declaring a String Variable and Storing String Value (Constant) in it
''' 

text = "Hazrat Muhammad (S.A.W.W.) is the Last Prophet of Allah"
				
			

Storing Value in a Variable at Run Time using Assignment Statement

  • Store Value in a Variable at Run Time using Assignment Statement
  • Question
    • How can we store Value in a Variable at Run Time using Assignment Statement?
  • Answer
    • To store Value in a Variable at Run Time using Assignment Statement, follow the following Two Step Process
      • Step 1: Declare a Variable
      • Step 2: Store Value in the Variable declared in the Step 1 using input() Function on the Right Hand Side (RHS) of the Assignment Statement
    • Note
      • In Python Programming Language, the above two Steps are accomplished in
        • A Single Python Statement
  • input () Function
  • Definition
    • The input() Function takes User input (reads a Line) convert it in a String and return it
  • Purpose
    • The main purpose of input() Function is to
      • Take Input from User at Run Time (i.e., during the execution of a Python Program)
  • Syntax – input() Function
				
					input(prompt)
				
			
ParameterDescription
PromptA String representing a message (i.e., displayed to the User) before the input.
  • Default Input Value Considered by input() Function
  • Question
    • By default, what Data Type is considered by the input() Function when a User enters a Value at Run Time?
  • Answer
    • String Constant
  • Converting User Input to an Integer Value
  • Question
    • How can we convert a String Value entered by a User (at Run Time using input() Function) into an Integer Value?
  • Answer
    • Use int() Type Casting Function
  • Example - Converting User Input to an Integer Value
				
					# Take an Integer Value as input from the User

number = int(input("Enter an Integer Number: "))
				
			
				
					Enter an Integer Number: 66
				
			
  • Converting User Input to a Float Value
  • Question
    • How can we convert a String Value entered by a User (at Run Time using input() Function) into an Float Value?
  • Answer
    • Use float() Type Casting Function
  • Example - Converting User Input to a Float Value
				
					# Take a Float Value as input from the User

number = float(input("Enter a Float Number: "))
				
			
				
					Enter an Integer Number: 66Enter a Float Number: 60.75
				
			
  • Code Time Variable Value vs Run Time Variable Value
  • Code Time Variable Value
    • At Code Time, the Value to be stored in a Variable is
      • Decided by the Programmer
    • Run Time Variable Value
      • At Run Time, the Value to be stored in a Variable is
        • Decided by the User (of the Program / Software)
      • Note
        • When we store Value in a Variable at Run Time
          • We must give a Meaningful Text Message to the User to tell him what Type of Value / Data s(he) should Enter
  • Example 1 - Declaring and Storing Integer Value in an Integer Variable at Run Time using Assignment Statement
				
					'''
Declaring an Integer Variable and Storing Integer Value (Constant) in it
'''

number = int(input("Enter an Integer Number: "))
				
			
				
					Enter an Integer Number: 20
				
			
  • Example 2 - Declaring and Storing Float Value in a Float Variable at Run Time using Assignment Statement
				
					'''
Declaring a Float Variable and Storing Float Value (Constant) in it
'''

number = float(input("Enter a Float Number: "))
				
			
				
					Enter a Float Number: 3.86
				
			
  • Example 3 - Declaring and Storing String Value in a String Variable at Run Time using Assignment Statement
				
					'''
Declaring a String Variable and Storing String Value (Constant) in it
'''

text = input("Enter a String: ")
				
			
				
					Enter a String: Hazrat Muhammad S.A.W.W. said, Allah will not be merciful to those who are not merciful to mankind.
				
			

TODO and Your Turn

Todo Tasks
Your Turn Tasks
Todo Tasks

TODO Task 1

  • Task
    • Consider the following Tasks and answer the questions given below
  • Note
    • Your answer should be
      • Well Justified
  • Questions
    • Write Python Programs for all the Tasks given below by following the Template given in this Chapter i.e.,
      • Completely and Correctly Understand the Task
        • Code

 

                                                                        Task  1

Completely and Correctly Understand the Task

Task Description

  • Write a Program in Python which declares and Store an Integer Value (i.e., 1200) in an Integer Variable at Code Time using Assignment Statement. Note that the name of Integer Variable should be int_ number.

  

                                                                        Task  2

Completely and Correctly Understand the Task

Task Description

  • Write a Program in Python which declares and Store a Float Value (i.e., 120.76) in a Float Variable at Code Time using Assignment Statement. Note that the name of Float Variable should be float_ number.

  

                                                                        Task  3

Completely and Correctly Understand the Task

Task Description

  • Write a Program in Python which declares and Store a String Value (i.e., The beauty of Sajood is you whisper down on Earth and It’s heard up in Heaven) in a String Variable at Code Time using Assignment Statement. Note that the name of String Variable should be str_ message.

  

                                                                        Task  4

Completely and Correctly Understand the Task

Task Description

  • Write a Program in Python which declares and Store an Integer Value in an Integer Variable at Run Time using Assignment Statement. Note that the name of Integer Variable should be int_ number.

Sample Output

Enter an Integer Number: 150

  

                                                                        Task  5

Completely and Correctly Understand the Task

Task Description

  • Write a Program in Python which declares and Store a Float Value in a Float Variable at Run Time using Assignment Statement. Note that the name of Float Variable should be float_ number.

Sample Output

Enter a Float Number: 120.65

 

                                                                        Task  6

Completely and Correctly Understand the Task

Task Description

  • Write a Program in Python which declares and Store a String Value in a String Variable at Run Time using Assignment Statement. Note that the name of String Variable should be str_ message.

Sample Output

Enter a String: The Holy Prophet S.A.W.W. said,   When Allah had finished His creation, He wrote over his Throne: ‘My Mercy preceded My Anger.’ 

Your Turn Tasks

Your Turn Task 1

  • Task
    • Write at least 6 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.,
      • Code

Use Value of a Variable for various Operations

  • Usage of Value Stored in a Variable
  • We can use Value stored in a Variable in various Python Statements, for e.g.,
    • In a print() Statement
    • In an Assignment Statement to Modify / Update the value stored in a Variable
    • And many more 😊
  • Note
    • In Sha Allah, in this Chapter, we will show how to use Value stored in a Variable
      • In a print() Statement
      • In an Assignment Statement to Modify / Update the value stored in a Variable
  • Access Value Stored in a Variable to Perform Various Operations
  • Question
    • How can we access Value stored in a Variable to Perform Various Operations?
  • Answer
    • Use Variable Name
  • Example 1 – Accessing Value Stored in an Integer Variable
				
					# Declare an Integer Variable and Store Value 7 in it 
number = 7

'''
(1) Accessing Value Stored in Variable using Variable Name (i.e., number) and (2) Displaying it on the Output Screen using print() Function
'''
print("Value stored in Variable is: ",number)
				
			
				
					Value stored in Variable is: 7
				
			
  • Example 2 – Accessing Value Stored in a Float Variable
				
					# Declare a Float Variable and Store Value 2.75 in it 
number = 2.75

'''
(1) Accessing Value Stored in Variable using Variable Name (i.e., number) and (2) Displaying it on the Output Screen using print() Function
'''

print("Value stored in Variable is: ", number)
				
			
				
					Value stored in Variable is: 2.75
				
			
  • Example 3 – Accessing Value Stored in a String Variable
				
					# Declare a String Variable and Store String in it 
message = " روزانہ کوئی ایک اچھا کام اللہ کی رضا کے لیے کریں "

'''
(1) Accessing Value Stored in Variable using Variable Name (i.e., number) and (2) Displaying it on the Output Screen using print() Function
'''

print("Message of the Day: " + message)
				
			
				
					Message of the Day: روزانہ کوئی ایک اچھا کام اللہ کی رضا کے لیے کریں
				
			
  • Analysis – Example 1, Example 2 and Example 3
  • In above three Examples e., Example 1, Example 2 and Example 3
    • Values are stored (in Variables) at Code Time
  • In Sha Allah, in the next Slides, I will give Examples, in which Values are stored (in Variables) at Run Time and then
    • Stored Values are displayed on the Output Screen using print() Function
  • Example 1 - Use Value Stored in a Variable in print() Function
				
					'''
Declaring an Integer Variable and Storing Integer Value (Constant)
'''

number = int(input("Enter an Integer Number: "))


'''
(1) Accessing Value Stored in Variable (number) using Variable Name (i.e., number) and (2) Displaying it on the Output Screen using print() Function
'''

print("Value Stored in Integer Variable (number) is: ", number)
				
			
				
					Enter an Integer Number: 100
Value Stored in Integer Variable (number) is: 100
				
			
  • Example 2 - Use Value Stored in a Variable in print() Function
				
					'''
Declaring a Float Variable and Storing Float Value (Constant)
'''

number = float(input("Enter a Float Number: "))

'''
(1) Accessing Value Stored in Variable (number) using Variable Name (i.e., number) and (2) Displaying it on the Output Screen using print() Function
'''

print("Value Stored in Float Variable (number) is: ", number)
				
			
				
					Enter a Float Number: 56.25
Value Stored in Float Variable (number) is: 56.25
				
			
  • Example 3 - Use Value Stored in a Variable in print() Function
				
					'''
Declaring a String Variable and Storing String Value (Constant)
'''

text = input("Enter a String: ")

'''
(1) Accessing Value Stored in Variable (number) using Variable Name (i.e., number) and (2) Displaying it on the Output Screen using print() Function
'''

print("Value Stored in String Variable (text) is: ", text)
				
			
				
					Enter a String: Just change one person in the world i.e., Yourself
Value Stored in String Variable (text) is: Just change one person in the world i.e., Yourself
				
			
  • Comparison – Accessing Value Stored (in a Variable) at Code Time vs Accessing Values Stored (in a Variable) at Run Time
  • From above Examples, it can be noted that whether we store Value (in a Variable) at Code Time or Run Time, the Value stored in the Variable is accessed in the same way i.e.,
    • Using Variable Name

TODO and Your Turn

Todo Tasks
Your Turn Tasks
Todo Tasks

TODO Task 1

  • Task
    • Consider the following Tasks and answer the questions given below
  • Note
    • Your answer should be
      • Well Justified
  • Questions
    • Write Python Programs for all the Tasks given below by following the Template given in this Chapter i.e.,
      • Completely and Correctly Understand the Task
        • Code

 

                                                                        Task  1

Completely and Correctly Understand the Task

Task Description

  • Write a Program in Python which Access Value Stored in an Integer Variable (at runtime) and Display the result on the Output Screen. Note that the name of Integer Variable should be int_ number.

Sample Output

Enter an Integer Value: 10

The Value stored in an Integer Variable (int_number) is: 10

 

                                                                        Task  2

Completely and Correctly Understand the Task

Task Description

  • Write a Program in Python which Access Value Stored in a Float Variable (at runtime) and Display the result on the Output Screen. Note that the name of Float Variable should be float_ number.

Sample Output

Enter a Float Value: 50.65

The Value stored in a Float Variable (float_number) is: 50.65  

 

                                                                        Task  3

Completely and Correctly Understand the Task

Task Description

  • Write a Program in Python which Access Value Stored in a String Variable (at runtime) and Display the result on the Output Screen. Note that the name of String Variable should be str_ message.

Sample Output

Enter a String: Holy Prophet S.A.W.W. said, Allah said, ‘I am to my slave as he thinks of Me, (i.e., I am able to do for him what he thinks I can do for him)

The Value stored in a String Variable (str_message) is: Holy Prophet S.A.W.W. said, Allah said, ‘I am to my slave as he thinks of Me, (i.e., I am able to do for him what he thinks I can do for him)

 

                                                                        Task  4

Completely and Correctly Understand the Task

Task Description

  • Write a Program in Python which Uses Value Stored in an Integer Variable (at runtime) and Display the result on the Output Screen using print() Function. Note that the name of Integer Variable should be int_ number.

Sample Output

Enter an Integer Value: 1000

The Value stored in an Integer Variable (int_number) is: 1000

 

                                                                        Task  5

Completely and Correctly Understand the Task

Task Description

  • Write a Program in Python which Uses Value Stored in a Float Variable (at runtime) and Display the result on the Output Screen using print() Function. Note that the name of Float Variable should be float_ number.

Sample Output

Enter a Float Value: 5.20

The Value stored in a Float Variable (float_number) is: 5.20

 

                                                                        Task  6

Completely and Correctly Understand the Task

Task Description

  • Write a Program in Python which Uses Value Stored in a String Variable (at runtime) and Display the result on the Output Screen using print() Function. Note that the name of String Variable should be str_ number.

Sample Output

Enter a String: Holy Prophet S.A.W.W. said, Enjoining, all that is good is a Sadaqa. 

The Value stored in a String Variable (str_message) is: The Prophet said, Enjoining, all that is good is a Sadaqa. 

 

Your Turn Tasks

Your Turn Task 1

  • Task
    • Write at least 6 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.,
      • Code

Modify / Update Value of a Variable using Assignment Statement

  • Modify / Update Value of a Variable
  • Question
    • How can we Modify / Update Value of a Variable?
  • Answer
    • Use Assignment Statement
  • Note
    • When we Modify / Update Value of a Variable, the Current Value (stored in the Variable) is Overwritten with the New Value i.e.,
      • A Single Variable can only store One Value at a Time 😊
    • In the Assignment Statement (used to Modify / Update Value of a Variable)
      • The Variable (whose Value we want to Update / Modify) must appear on the
        • Left Hand Side (LHS) of the Assignment Statement
      • On the Right Hand Side (RHS) of the Assignment Statement we may have
        • Constant / Variable / Expression / Function
  • Dry Run
  • Definition
    • The testing process which reduces the effect of Failure is called Dry Run
  • Purpose
    • The main purpose of a Dry Run is to
      • Trace the Value of Variables
    • Importance
      • Dry run can be carried out during
        • Design
        • Implementation
        • Testing or
        • Maintenance
      • used to identify logic errors
      • they will not find execution errors
    • Applications
      • Dry runs help in
        • Identify Error in Values at Code Time
        • Discover Sections causing those Errors
      • Note
        • In Sha Allah, in the next Examples, we will show how to Dry Run Python Programs
  • Example 1 - Modify / Update Value of Integer Variables using Assignment Statement
				
					'''
Declaring two Integer Variables and Storing Integer Values (Constants) in them
'''
 
number1 = int(input("Enter First Integer Number: "))
number2 = int(input("Enter Second Integer Number: "))
 
# Display values of number1 and number2
print("Value of number1 = ", number1)
print("Value of number2 = ", number2)
 
# Modify / Update Value of Variable (number1)
number1 = 100 - 10
 
# Modify / Update Value of Variable (number2)
number2 = number1 * number1 
 
# Display updated values of number1 and number2
print("Updated Value of number1 = ", number1)
print("Updated Value of number2 = ", number2)
				
			

Python Statement

Output Screen

Variable(s)

number1 = int(input(“Enter First Integer Number: ”))

Enter First Integer Number: 20

number1 = 20

number2 = int(input(“Enter Second Integer Number: ”))

Enter First Integer Number: 20

Enter Second Integer Number: 60

number1 = 20

number2 = 60

print(“Value of number1 = “, number1)

Enter First Integer Number: 20

Enter Second Integer Number: 60

Value of number1 = 20

number1 = 20

number2 = 60

print(“Value of number2 = “, number2)

Enter First Integer Number: 20

Enter Second Integer Number: 60

Value of number1 = 20

Value of number2 = 60

number1 = 20

number2 = 60

number1 = 100 – 10

Enter First Integer Number: 20

Enter Second Integer Number: 60

Value of number1 = 20

Value of number2 = 60

number1 = 90

number2 = 60

number2 = number1 * number1

Enter First Integer Number: 20

Enter Second Integer Number: 60

Value of number1 = 20

Value of number2 = 60

number1 = 90

number2 = 8100

print(“Updated Value of number1 = “, number1)

Enter First Integer Number: 20

Enter Second Integer Number: 60

Value of number1 = 20

Value of number2 = 60

Updated Value of number1 = 90

number1 = 90

number2 = 8100

print(“Updated Value of number2 = “, number2)

Enter First Integer Number: 20

Enter Second Integer Number: 60

Value of number1 = 20

Value of number2 = 60

Updated Value of number1 = 90

Updated Value of number2 = 8100

number1 = 90

number2 = 8100

				
					Enter First Integer Number: 20
Enter Second Integer Number: 60
Value of number1 = 20
Value of number2 = 60
Updated Value of number1 = 90
Updated Value of number2 = 8100
				
			
  • Example 2 - Modify / Update Value of Float Variables using Assignment Statement
				
					'''
Declaring two Float Variables and Storing Float Values (Constants) in them
'''
 
number1 = float(input("Enter First Float Number: "))
number2 = float(input("Enter Second Float Number: "))
 
# Display values of number1 and number2
print("Value of number1 = ", number1)
print("Value of number2 = ", number2)
 
# Modify / Update Value of Variable (number1)
number1 = 10.50 - 5.75
 
# Modify / Update Value of Variable (number2)
number2 = number1 * number1 
 
# Display updated values of number1 and number2
print("Updated Value of number1 = ", number1)
print("Updated Value of number2 = ", number2)
				
			

Python Statement

Output Screen

Variable(s)

number1 = float(input(“Enter First Float Number: ”))

Enter First Float Number: 10.65

number1 = 10.65

number2 = float(input(“Enter Second Float Number: ”))

Enter First Float Number: 10.65

Enter Second Float Number: 20.45

number1 = 10.65

number2 = 20.45

print(“Value of number1 = “, number1)

Enter First Float Number: 10.65

Enter Second Float Number: 20.45

Value of number1 = 10.65

number1 = 10.65

number2 = 20.45

print(“Value of number2 = “, number2)

Enter First Float Number: 10.65

Enter Second Float Number: 20.45

Value of number1 = 10.65

Value of number2 = 20.45

number1 = 10.65

number2 = 20.45

number1 = 10.50 – 5.75

Enter First Float Number: 10.65

Enter Second Float Number: 20.45

Value of number1 = 10.65

Value of number2 = 20.45

number1 = 4.75

number2 = 20.45

number2 = number1 * number1

Enter First Float Number: 10.65

Enter Second Float Number: 20.45

Value of number1 = 10.65

Value of number2 = 20.45

number1 = 4.75

number2 = 22.57

print(“Updated Value of number1 = “, number1)

Enter First Float Number: 10.65

Enter Second Float Number: 20.45

Value of number1 = 10.65

Value of number2 = 20.45

Updated Value of number1 = 4.75

number1 = 4.75

number2 = 22.57

print(“Updated Value of number2 = “, number2)

Enter First Float Number: 10.65

Enter Second Float Number: 20.45

Value of number1 = 10.65

Value of number2 = 20.45

Updated Value of number1 = 4.75

Updated Value of number2 = 22.57

number1 = 4.75

number2 = 22.57

				
					Enter First Float Number: 10.65
Enter Second Float Number: 20.45
Value of number1 = 10.65
Value of number2 = 20.45
Updated Value of number1 = 4.75
Updated Value of number2 = 22.57
				
			
  • Example 3 - Modify / Update Value of an String Variable using Assignment Statement
				
					text1 = input("Enter First String Message: ")
text2 = input("Enter Second String Message: ")
 
# Display values of text1 and text2
print("Value of text1 = ", text1)
print("Value of text2 = ", text2)
 
# Modify / Update Value of Variable (text1)
text1 = "Three prayers are answered without doubt: A prayer by a person suffering injustice, and a prayer by a traveler, and a prayer by a father for his children. "
 
# Modify / Update Value of Variable (text2)
text2 = "Allah’s Pleasure lies in the pleasure of one’s parents; while His Wrath too lies in the wrath of theirs. " 
 
# Display updated values of text1 and text2
print("Updated Value of text1 = ", text1)
print("Updated Value of text2 = ", text2)
				
			

Python Statement

Output Screen

Variable(s)

text1 = input(“Enter First String Message: ”))

Enter First String Message: Reciting excessive Durood Shareef brings purity

text1 = Reciting excessive Durood Shareef brings purity

text2 = input(“Enter Second String Message:”))

Enter First String Message: Reciting excessive Durood Shareef brings purity

Enter Second String Message: Reciting Durood in this world is rewarding in the next

text1 = Reciting excessive Durood Shareef brings purity

text2 = Reciting Durood in this world is rewarding in the next

print(“Value of text1 = “, text1)

Enter First String Message: Reciting excessive Durood Shareef brings purity

Enter Second String Message: Reciting Durood in this world is rewarding in the next

Value of text1 = Reciting excessive Durood Shareef brings purity

text1 = Reciting excessive Durood Shareef brings purity

text2 = Reciting Durood in this world is rewarding in the next

print(“Value of text2 = “, text2)

Enter First String Message: Reciting excessive Durood Shareef brings purity

Enter Second String Message: Reciting Durood in this world is rewarding in the next

Value of text1 = Reciting excessive Durood Shareef brings purity

Value of text2 = Reciting Durood in this world is rewarding in the next

text1 = Reciting excessive Durood Shareef brings purity

text2 = Reciting Durood in this world is rewarding in the next

text1 = “Allah showers ten blessings on the man who recites a Durood.”

Enter First String Message: Reciting excessive Durood Shareef brings purity

Enter Second String Message: Reciting Durood in this world is rewarding in the next

Value of text1 = Reciting excessive Durood Shareef brings purity

Value of text2 = Reciting Durood in this world is rewarding in the next

text1 = Allah showers ten blessings on the man who recites a Durood.

text2 = Reciting Durood in this world is rewarding in the next

text2 = “When the night of Jummah arrives, increase your recital of the Durood Shareef”

Enter First String Message: Reciting excessive Durood Shareef brings purity

Enter Second String Message: Reciting Durood in this world is rewarding in the next

Value of text1 = Reciting excessive Durood Shareef brings purity

Value of text2 = Reciting Durood in this world is rewarding in the next

text1 = Allah showers ten blessings on the man who recites a Durood.

text2 = When the night of Jummah arrives, increase your recital of the Durood Shareef

print(“Updated Value of text1 = “, text1)

Enter First String Message: Reciting excessive Durood Shareef brings purity

Enter Second String Message: Reciting Durood in this world is rewarding in the next

Value of text1 = Reciting excessive Durood Shareef brings purity

Value of text2 = Reciting Durood in this world is rewarding in the next

Updated Value of text1 = Allah showers ten blessings on the man who recites a Durood.

text1 = Allah showers ten blessings on the man who recites a Durood.

text2 = When the night of Jummah arrives, increase your recital of the Durood Shareef

print(“Updated Value of text2 = “, text2)

Enter First String Message: Reciting excessive Durood Shareef brings purity

Enter Second String Message: Reciting Durood in this world is rewarding in the next

Value of text1 = Reciting excessive Durood Shareef brings purity

Value of text2 = Reciting Durood in this world is rewarding in the next

Updated Value of text1 = Allah showers ten blessings on the man who recites a Durood.

Updated Value of text2 = When the night of Jummah arrives, increase your recital of the Durood Shareef

text1 = Allah showers ten blessings on the man who recites a Durood.

text2 = When the night of Jummah arrives, increase your recital of the Durood Shareef

 

				
					Enter First String Message: Reciting excessive Durood Shareef brings purity
Enter Second String Message: Reciting Durood in this world is rewarding in the next
Value of text1 = Reciting excessive Durood Shareef brings purity
Value of text2 = Reciting Durood in this world is rewarding in the next
Updated Value of text1 = Allah showers ten blessings on the man who recites a Durood.
Updated Value of text2 = When the night of Jummah arrives, increase your recital of the Durood Shareef
				
			

TODO and Your Turn

Todo Tasks
Your Turn Tasks
Todo Tasks

TODO Task 1

  • Task
    • Consider the following Tasks and answer the questions given below
  • Note
    • Your answer should be
      • Well Justified
  • Questions
    • Write Python Programs for all the Tasks given below by following the Template given in this Chapter i.e.,
      • Completely and Correctly Understand the Task
        • Code
        • Execution of Code (Dry Run)
        • Output of Code
            •  

                                                                        Task  1

Completely and Correctly Understand the Task

Task Description

  • Write a Program in Python which performs the following Operations and Display the result on the Output Screen
    • Stores Integer Values
    • Modify / Update Values of Integer Variables using Assignment Statement
    • Note that the name of Integer Variables should be number1 and number2

Sample Output

Enter First Integer Number: 500
Enter Second Integer Number: 600
Value of number1 = 500
Value of number2 = 600
Updated Value of number1 = 25 (i.e., 500 / 20)
Updated Value of number2 = -25 (i.e., 600 – number1 * number1)

 

                                                                        Task  2

Completely and Correctly Understand the Task

Task Description

  • Write a Program in Python which performs the following Operations and Display the result on the Output Screen
    • Stores Float Values
    • Modify / Update Values of a Float Variables using Assignment Statement
    • Note that the name of Float Variables should be number1 and number2.

Sample Output

Enter First Float Number: 45.52

Enter Second Float Number: 55.59

Value of number1 = 45.52

Value of number2 = 55.59

Updated Value of number1 = -10.07 (i.e., 45.52 – 55.59)

Updated Value of number2 = 101.40 (i.e., 10.47 * 10.47)

 

                                                                        Task  3

Completely and Correctly Understand the Task

Task Description

  • Write a Program in Python which performs the following Operations and Display the result on the Output Screen
    • Stores String Values
    • Modify / Update Values of a String Variables using Assignment Statement
    • Note that the name of String Variables should be text1 and text2.

Sample Output

Enter First String Message: The Holy Prophet S.A.W.W. said, abusing a Muslim is Fusuq (i.e., an evil-doing), and killing him is Kufr (disbelief).

Enter Second String Message: The Holy Prophet S.A.W.W. said, He who is not merciful to others, will not be treated mercifully.

Value of text1 = The Holy Prophet S.A.W.W. said, abusing a Muslim is Fusuq (i.e., an evil-doing), and killing him is Kufr (disbelief).

Value of text2 = The Holy Prophet S.A.W.W. said, He who is not merciful to others, will not be treated mercifully.

Updated Value of text1 = The Prophet S.A.W.W said, if any Muslim plants any plant and a human being or an animal eats of it, he will be rewarded as if he had given that much in charity.

Updated Value of text2 = The Holy Prophet S.A.W.W. said, Gabriel continued to recommend me about treating the neighbors Kindly and politely so much so that I thought he would order me to make them as my heirs.

 

Your Turn Tasks

Your Turn Task 1

  • Task
    • Write at least 6 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.,
      • Code
      • Output

Check Data Type of a Variable

  • Check Data Type of a Variable
  • Question
    • How can we check Data Type of a Variable?
  • Answer
    • Use type() Function
  • Syntax - type() Function
  • The Syntax of type() Function is as follows
				
					type(object) # Object whose Data Type needs to be checked
				
			
  • Example 1 - Check Data Type of an Integer Variable
				
					'''Declaring and Storing Integer Value (Constant) in an Integer Variable'''
 
number = int(input("Enter an Integer Number: ")) 
 
'''Check Data Type of Integer Variable (number) using type() Function'''
 
print("Data Type of Variable (number) is: ", type(number))
				
			
				
					Enter an Integer Number: 20000
Data Type of Variable (number) is: <class 'int'>
				
			
  • Example 2 - Check Data Type of a Float Variable
				
					'''Declaring and Storing Float Value (Constant) in a Float Variable'''
 
number = float(input("Enter a Float Number: "))
 
'''Check Data Type Float of Variable (number) using type() Function'''
 
print("Data Type of Variable (number) is: ", type(number))
				
			
				
					Enter a Float Number: 12.67
Data Type of Variable (number) is: <class 'float'>
				
			
  • Example 3 - Check Data Type of a String Variable
				
					'''Declaring and Storing String Value (Constant) in a String Variable''' 
 
text = input("Enter a String: ")
 
'''Check Data Type of String Variable (text) using type() Function'''
 
print("Data Type of Variable (text) is: ", type(text))
				
			
				
					Enter a String: No matter how many prayers you have missed, don’t miss the next one
Data Type of Variable (text) is: <class 'str'>
				
			

TODO and Your Turn

Todo Tasks
Your Turn Tasks
Todo Tasks

TODO Task 1

  • Task
    • Consider the following Tasks and answer the questions given below
  • Note
    • Your answer should be
      • Well Justified
  • Questions
    • Write Python Programs for all the Tasks given below by following the Template given in this Chapter i.e.,
      • Completely and Correctly Understand the Task
        • Code
        • Output of Code

 

                                                                        Task  1

Completely and Correctly Understand the Task

Task Description

o   Write a Program in Python which performs the following Operations and Display the result on the Output Screen

o   Stores an Integer Value

o   Check Data Type of the Variable

Sample Output

Enter an Integer Value: 1000

Data Type of Integer Variable (number) is: <class ‘int’>

  

                                                                        Task  2

Completely and Correctly Understand the Task

Task Description

o   Write a Program in Python which performs the following Operations and Display the result on the Output Screen

o   Stores a Float Value

o   Check Data Type of the Variable

Sample Output

Enter a Float Value: 100.00

Data Type of Float Variable (number) is: <class ‘float’>

  

                                                                        Task  3

Completely and Correctly Understand the Task

Task Description

o   Write a Program in Python which performs the following Operations and Display the result on the Output Screen

o   Stores a String Value

o   Check Data Type of the Variable

Sample Output

Enter a String: The Holy Prophet S.A.W.W. said, On the Day of Qiyamah, at all places the closest to me will be those that have read the greatest number of Durood.

Data Type of String Variable (message) is: <class ‘string’>

 

Your Turn Tasks

Your Turn Task 1

  • Task
    • Write at least 6 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.,
      • Code
      • Output

Check Memory Address of a Variable

  • Check Memory Address of a Variable
  • Question
    • How can we check the Memory Address of a Variable?
  • Answer
    • Use id() Function 
  • Note
    • The id() Function returns an Integer (Unique ID), which 
    • Represents the Virtual Address of a Variable
  • Syntax – id() Function
				
					(object)  # Object whose Memory Address needs to be checked
				
			
  • Example 1 – Check Memory Address of an Integer Variable
				
					'''Declare an Integer Variable and Store an Integer Value (Constant) in it'''

number = int(input("Enter an Integer Number: "))

'''Check Memory Address of an Integer Variable (number) using id() Function'''

print("Memory Address of Variable (number) is: ", id(number))

				
			
				
					Enter an Integer Number: 13500
Memory Address of Variable (number) is: 9079040

				
			
  • Example 2 - Check Memory Address of a Float Variable
				
					'''Declare a Float Variable and Store a Float Value (Constant) in it'''

number = float(input("Enter a Float Number: "))

'''Check Memory Address of a Float Variable (number) using id() Function'''

print("Memory Address of Variable (number) is: ", id(number))

				
			
				
					Enter a Float Number: 1200.22
Memory Address of Variable (number) is: 47542575264416
				
			
  • Example 3 - Check Memory Address of a String Variable
				
					'''Declare a String Variable and Store an String Value (Constant) in it'''

text = input("Enter a String: ")

'''Check Memory Address of a String Variable (text) using id() Function''' 

print("Memory Address of Variable (text) is: ", id(text))

				
			
				
					Enter a String: Holy Prophet S.A.W.W. said, On the Day of Qiyamah, at all places the closest to me will be those that have read the greatest number of Durood
Memory Address of Variable (text) is: 47323107897968

				
			
  • Example 4 - Check Memory Address of a String Variable
				
					'''Declare a String Variable and Store a String Value (Constant) in it'''

character = input("Enter any Special Character: ")

'''Check Memory Address of a String Variable (character) using id() Function''' 

print("Memory Address of Variable (character) is: ", id(character))

				
			
				
					Enter any Special Character: #
Memory Address of Variable (character) is: 47210468026888
				
			

TODO and Your Turn

Todo Tasks
Your Turn Tasks
Todo Tasks

TODO Task 1

  • Task
    • Consider the following Tasks and answer the questions given below
  • Note 
    • Your answer should be 
      • Well Justified
  • Questions
    • Write Python Programs for all the Tasks given below by following the Template given in this Chapter i.e., 
      • Completely and Correctly Understand the Task
        • Code
        • Output of Code
    • Note
      • The Memory Addresses uses in the Sample Output are Dummy Values
  •  
    •  

                        Task  1

Completely and Correctly Understand the Task

Task Description

  • Write a Program in Python which performs the following Operations and Display the result on the Output Screen

·        Stores an Integer Value

·        Check Memory Address of an Integer Variable

Sample Output

Enter an Integer Value: 10000

Memory Address of Variable (number) is: 57323107897968

                        Task  2

Completely and Correctly Understand the Task

Task Description

  • Write a Program in Python which performs the following Operations and Display the result on the Output Screen

·        Stores a Float Value

·        Check Memory Address of a Float Variable

Sample Output

Enter a Float Value: 100.999

Memory Address of Variable (number) is: 77323107897968

                        Task  3

Completely and Correctly Understand the Task

Task Description

  • Write a Program in Python which performs the following Operations and Display the result on the Output Screen

·        Stores a String Value

·        Check Memory Address of a String Variable

Sample Output

Enter a String:  The Holy Prophet S.A.W.W. said, He who reads the Durood Shareef 50 times daily, I shall compliment him on the Day of Qiyamah

Memory Address of Variable (text) is: 87323107897968

Your Turn Tasks

Your Turn Task 1

  • Task
    • Write at least 6 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.,
        • Code 
        • Output

Variable Naming Conventions – Best Coding Practices

  • Best Coding Practices
  • For more details on Best Coding Practices
    • See Chapter 9 – Best Coding Practices
  •  
  • Variable Naming Conventions – Best Coding Practices
  • Best Coding Practice 01 
    • Variable Name should be in lower case
				
					#  Bad Coding Practices 
INTNUMBER = 100
FLOATNUMBER = 12.50
MESSAGE = "Five Namaz are Obligatory on every Muslim."

# Good Coding Practices
number1 = 20 
number2 = 3.86
message = "You cannot go back and change the beginning, but you can start where you are and change the ending."
				
			
  • Variable Naming Conventions – Best Coding Practices
  • Best Coding Practices 02
    • Use expressive Variable Names
  • Note 
    • Avoid using single Character (for e.g., x, a, I, j etc.) Variable Name
    • Avoid Variable Names that begin with a number
    • Avoid using Special Characters (@,!,#,$) in Variable Names
  •  
				
					#  Bad Coding Practices
a = 0
1number = 1000
number! = 10.25
@message1 = "The Holy Prophet S.A.W.W. said, He who sends a single Durood upon me, Almighty Allah rewards him ten times and ten good deeds are recorded in his book of good deeds"

# Good Coding Practices
count = 0
number1 = 10
number2 = 100.65
message = "Hazrat Muhammad S.A.W.W. said, He who reads the Durood Shareef in abundance will be blessed with many beautiful Maidens as wives in Jannah"
				
			
  • Variable Naming Conventions – Best Coding Practices
  • Best Coding Practices 03
    • Use underscore to separate words in long Variable Names
				
					# Bad Coding Practices
integernumber = 20 
floatnumber = 3.86
stringmessage = "You cannot go back and change the beginning, but you can start where you are and change the ending."

# Good Coding Practices
integer_number = 20 
float_number = 3.86
string_message = "You cannot go back and change the beginning, but you can start where you are and change the ending."
				
			

Python Programs with Integer Variables

  • Python Programs with Integer Variables
  • In sha Allah, in the next Slides, we will write different Python Programs that use Integer Variables
    • Also, we will provide TODO Tasks and Your Turn Tasks for further Practice  
  • Remember 
    • To Master any Art, the only Key is 
      • Practice (by giving 100% Effort with Sincerity) under the supervision of a Guru 😊
  • Task 1
Completely and Correctly Understand the Task
Task Description
  • Write a Program in Python which stores an Integer Value (i.e., 7) in an Integer Variable (at Run Time) and displays the Value, Data Type and Memory Address of Integer Variable on the Output Screen. Note that the name of Integer Variable should be no_of_days.
Sample Output

In how many days Allah created the heaven and earth? 7

Allah created heavens and earth in 7 days

Data Type of no_of_days: <class ‘int’>

Memory Address of no_of_days: 9079200

Plan and Design Solution to the Task
Input-Processing-Output
  • Input
    • Data
      • An Integer Value (i.e., 7) stored in an Integer Variable (no_of_days)
    • Instruction(s)
      • Display Value, Data Type and Memory Address of the Variable (no_of_days) on the Output Screen 
  • Output
    • Value, Data Type and Memory Address of the Variable (no_of_days) displayed on the Output Screen 
  • Processing
    • Use print() Function – To display Integer Value (stored in the Variable) on the Output Screen
    • Use type() Function – To Display Data Type of Variable on the Output Screen
    • Use id() Function – To Display Memory Address of Variable on the Output Screen
Implementation in Python
Code
				
					'''
__author__   = Ms. Samavi Salman
__copyright__= Copyright (C) 2020 Ms. Samavi Salman
__license__  = Public Domain
__version__  = 1.0
'''

# Input 

# Take Integer Value as Input from User and store in Integer Variable (no_of_days)

no_of_days = int(input("In how many days Allah created the heavens and earth? "))

# Processing + Output

# Display Value stored in no_of_days
 
print("Allah created heavens and earth in", no_of_days, "days")

# Display Data Type of no_of_days 

print("Data Type of Integer Variable is:", type(no_of_days))

# Display Memory Address of no_of_days
 
print("Memory Address of Integer Variable is:", id(no_of_days))
				
			
				
					In how many days Allah created the heavens and earth? 7
Allah created heavens and earth in 7 days
Data Type of Integer Variable is: <class 'int'>
Memory Address of Integer Variable is: 93858896399072
				
			
Analysis, Main Findings and Conclusions
Analysis of Python Program
  • In this Python Program, I displayed the Value, Data Type and Memory Address of Integer Variable on the Output Screen using 

    • print() Function
    • type() Function
    • id() Function
Main Findings
  • Alhamdulillah (الحمدللہ) 😊, print(), type() and id() Functions efficiently and effectively performed my required Task i.e.,
    • Displaying Value, Data Type and Memory Address of Integer Variable on the Output Screen
Conclusions
  • To conclude, our main goal i.e.
    • Displaying Value, Data Type and Memory Address of Integer Variable on the Output Screen was successfully accomplished using print(), type() and id() Functions. Alhamdulillah (الحمدللہ) 😊
  • Task 2
Completely and Correctly Understand the Task
Task Description
  • Write a Program in Python which stores two Integer Values (i.e., 11 and 4) in two Integer Variables (at Run Time) and displays the Values, Data Types and Memory Addresses of the two Integer Variables on the Output Screen. Note that the name of Integer Variables should be no_of_wives and no_of_children.
Sample Output

How many wives Hazrat Muhammad S.A.W.W. had? 11

How many children Hazrat Muhammad S.A.W.W. had? 4

Hazrat Muhammad S.A.W.W. had 11 wives and 4 children

Data Type of Integer Variables are:  <class ‘int’> and <class ‘int’>

Memory Addresses of Integer Variables is:  93858896399072 and 93858896398976 

Plan and Design Solution to the Task
Input-Processing-Output
  • Input
    • Data
      • Two Integer Values (i.e., 11 and 4) stored in Integer Variables (no_of_wives and no_of_childern)
    • Instruction(s)
      • Display Values, Data Types and Memory Addresses of the Variables (no_of_wives and no_of_childern) on the Output Screen 
  • Output
    • Values, Data Types and Memory Addresses of the Variables (no_of_wives and no_of_childern) displayed on the Output Screen 
  • Processing
    • Use print() Function – To display Integer Values (stored in the Variables) on the Output Screen
    • Use type() Function – To Display Data Types of Variables on the Output Screen
    • Use id() Function – To Display Memory Addresses of Variables on the Output Screen
Implementation in Python
Code
				
					'''
__author__   = Ms. Samavi Salman
__copyright__= Copyright (C) 2020 Ms. Samavi Salman
__license__  = Public Domain
__version__  = 1.0
'''

# Input 

# Take Integer Values as Input from User and store in Integer Variables (no_of_wives and no_of_childern)

no_of_wives = int(input("How many wives Hazrat Muhammad S.A.W.W. had? "))
no_of_childern = int(input("How many children Hazrat Muhammad S.A.W.W. had? "))

# Processing + Output

# Display Values stored in no_of_wives and no_of_childern

print("Hazrat Muhammad S.A.W.W. had" ,no_of_wives, "wives and", no_of_childern ,"children")

# Display Data Types of no_of_wives and no_of_childern

print("Data Types of Integer Variables are:", type(no_of_wives), "and", type(no_of_childern))

# Display Memory Addresses of no_of_wives and no_of_childern

print("Memory Addresses of Integer Variables are: ", id(no_of_days), "and", id(no_of_childern)) 


				
			
				
					How many wives Hazrat Muhammad S.A.W.W. had? 11
How many children Hazrat Muhammad S.A.W.W. had? 4
Hazrat Muhammad S.A.W.W. had 11 wives and 4 children
Data Types of Integer Variables are:  <class 'int'> and <class 'int'>
Memory Addresses of Integer Variables are:  93858896399072 and 93858896398976
				
			
Analysis, Main Findings and Conclusions
Analysis of Python Program
  • In this Python Program, I displayed the Values, Data Types and Memory Addresses of Integer Variables on the Output Screen using 
    • print() Function
    • type() Function
    • id() Function
Main Findings
  • Alhamdulillah (الحمدللہ) 😊, print() Function efficiently and effectively performed my required Task i.e.,
    • Displaying Values, Data Types and Memory Addresses of Integer Variables on the Output Screen
Conclusions
  • To conclude, our main goal i.e.
    • Displaying Values, Data Types and Memory Addresses of Integer Variables on the Output Screen was successfully accomplished using print(), type() and id() Functions. Alhamdulillah (الحمدللہ) 😊

Python Programs with Float Variables

Steps - Real-world Task Performed by a Computer System

  • Python Programs with Float Variables
  • In sha Allah, in the next Slides, we will write different Python Programs that use Float Variables
    • Also, we will provide TODO Tasks and Your Turn Tasks for further Practice  
  • Remember 
    • To Master any Art, the only Key is 
      • Practice (by giving 100% Effort with Sincerity) under the supervision of a Guru 😊 
  •  
  • Task 1
Completely and Correctly Understand the Task
Task Description
  • Write a Program in Python which stores a Float Value (i.e., 2.5) in a Float Variable (at Run Time) and displays the Value, Data Type and Memory Address of a Float Variable on the Output Screen. Note that the name of Float Variable should be zakat.
Sample Output

What is Value of Zakat for Muslims in Percentage? 2.5

Value of Zakat for Muslims is 2.5 %

Data Type of Float Variable are:  <class ‘float’>

Memory Addresses of Float Variable is:  140319791418872

Plan and Design Solution to the Task
Input-Processing-Output
  • Input
    • Data
      • A Float Value (i.e., 2.5) stored in Float Variable (zakat)
    • Instruction(s)
      • Display Value, Data Type and Memory Address of the Variable (zakat) on the Output Screen 
  • Output
      • Value, Data Type and Memory Address of the Variable (zakat) displayed on the Output Screen 
  • Processing
    • Use print() Function – To display Float Value (stored in the Variables) on the Output Screen
    • Use type() Function – To Display Data Type of Variable on the Output Screen
    • Use id() Function – To Display Memory Address of Variable on the Output Screen
  •  
    •  
Implementation in Python
Code

 

				
					'''
__author__   = Ms. Samavi Salman
__copyright__= Copyright (C) 2020 Ms. Samavi Salman
__license__  = Public Domain
__version__  = 1.0
'''

# Input 

# Take Float Value as Input from User and store in Float Variable (zakat)

zakat = float(input("What is Value of Zakat for Muslims in Percentage? "))

# Processing + Output

# Display Values stored in zakat

print("Value of Zakat for Muslims is", zakat, "%")

# Display Data Types of zakat

print("Data Type of Float Variable is: ", type(zakat))

# Display Memory Addresses of zakat

print("Memory Addresses of Float Variable is: ", id(zakat))
				
			
				
					What is Value of Zakat for Muslims in Percentage? 2.5
Value of Zakat for Muslims is 2.5 %
Data Type of Float Variable is:  <class 'float'>
Memory Addresses of Float Variable is:  140319791418872
				
			
Analysis, Main Findings and Conclusions
Analysis of Python Program
  • In this Python Program, I displayed the Value, Data Type and Memory Address of Float Variable on the Output Screen using 
    • print() Function
    • type() Function
    • id() Function
Main Findings
  • Alhamdulillah (الحمدللہ) 😊, print() Function efficiently and effectively performed my required Task i.e.,
    • Displaying Value, Data Type and Memory Address of Float Variable on the Output Screen
Conclusions
  • To conclude, our main goal i.e.
    • Displaying Value, Data Type and Memory Address of Float Variable on the Output Screen was successfully accomplished using print(), type() and id() Functions. Alhamdulillah (الحمدللہ) 😊
  • Task 2
Completely and Correctly Understand the Task
Task Description
  • Write a Program in Python which stores Float Values (i.e., 10.5 and 110.25) in a Float Variables (at Run Time) and displays the Values, Data Types and Memory Addresses of a Float Variables on the Output Screen. Note that the name of Float Variables should be sadqa and reward.
Sample Output

Enter the amount of Sadqa: 10.5

Calculate the reward given by Allah on amount of Sadqa paid? 

If you give Sadqa of 10.5 rupees, the minimum amount that Allah will give you is:  110.25

Data Type of Float Variables are:  <class ‘float’> and <class ‘float’>

Memory Addresses of Float Variables is:  140288832121976 and 140288832122432

Plan and Design Solution to the Task
Input-Processing-Output
  • Input
    • Data
      • Two Float Values (i.e., 10.5 and 110.25) stored in Float Variables (sadqa and reward)
    • Instruction(s)
      • Display Values, Data Types and Memory Addresses of the Variables (sadqa and reward) on the Output Screen 
  • Output
    • Values, Data Types and Memory Addresses of the Variables (zakat) displayed on the Output Screen 
  • Processing
    • Use print() Function – To display Float Values (stored in the Variables) on the Output Screen
    • Use type() Function – To Display Data Types of Variables on the Output Screen
    • Use id() Function – To Display Memory Addresses of Variables on the Output Screen
Implementation in Python
Code

 

				
					'''
__author__   = Ms. Samavi Salman
__copyright__= Copyright (C) 2020 Ms. Samavi Salman
__license__  = Public Domain
__version__  = 1.0
'''

# Input 

# Take Float Values as Input from User and store in Float Variables (sadqa and reward)
sadqa = float(input("Enter the amount of Sadqa: "))
print("Calculate the reward given by Allah on amount of Sadqa paid")

reward = sadqa * sadqa

# Processing + Output

# Display Values stored in sadqa and reward
print("If you give Sadqa of ", sadqa , "rupees, the minimum amount that Allah will give you is: ", reward)

# Display Data Types of sadqa and reward
print("Data Types of Float Variables are: ", type(sadqa),"and",type(reward))

# Display Memory Addresses of sadqa and reward
print("Memory Addresses of Float Variables is: ", id(sadqa),"and",id(reward))
				
			
				
					Enter the amount of Sadqa: 10.5
Calculate the reward given by Allah on amount of Sadqa paid? 
If you give Sadqa of 10.5 rupees, the minimum amount that Allah will give you is:  110.25
Data Types of Float Variables are:  <class 'float'> and <class 'float'>
Memory Addresses of Float Variables is:  140288832121976 and 140288832122432
				
			
Analysis, Main Findings and Conclusions
Analysis of Python Program
  • In this Python Program, I displayed the Value, Data Type and Memory Address of Float Variable on the Output Screen using 
    • print() Function
    • type() Function
    • id() Function
Main Findings
  • Alhamdulillah (الحمدللہ) 😊, print() Function efficiently and effectively performed my required Task i.e.,
    • Displaying Value, Data Type and Memory Address of Float Variable on the Output Screen
Conclusions
  • To conclude, our main goal i.e.
    • Displaying Value, Data Type and Memory Address of Float Variable on the Output Screen was successfully accomplished using print(), type() and id() Functions. Alhamdulillah (الحمدللہ) 😊

Python Programs with String Variables

  • Python Programs with String Variables
  • In sha Allah, in the next Slides, we will write different Python Programs that use String Variables
    • Also, we will provide TODO Tasks and Your Turn Tasks for further Practice  
  • Remember 
    • To Master any Art, the only Key is 
      • Practice (by giving 100% Effort with Sincerity) under the supervision of a Guru 😊
  •  
  • Task 1
Completely and Correctly Understand the Task
Task Description
  • Write a Program in Python which stores String Value (i.e., Anjeer) in a String Variable (at Run Time) and displays the Value, Data Type and Memory Address of a String Variables on the Output Screen. Note that the name of String Variable should be fruit_of_jannah.
Sample Output

Name the Fruit of Jannah? Anjeer

Anjeer is the fruit of jannah

Data Type of String Variable are:  <class ‘str’>

Memory Address of String Variable is:  139731417268496

Plan and Design Solution to the Task
Input-Processing-Output
  • Input
    • Data
      • A String Value (i.e., Anjeer) stored in String Variable (fruit_of_jannah)
    • Instruction(s)
      • Display Value, Data Type and Memory Address of the Variables (fruit_of_jannah) on the Output Screen 
  • Output
    • Value, Data Type and Memory Address of the Variable (fruit_of_jannah) displayed on the Output Screen 
  • Processing
    • Use print() Function – To display String Value (stored in the Variables) on the Output Screen
    • Use type() Function – To Display Data Type of Variable on the Output Screen
    • Use id() Function – To Display Memory Address of Variable on the Output Screen
Implementation in Python
Code

 

				
					'''
__author__   = Ms. Samavi Salman
__copyright__= Copyright (C) 2020 Ms. Samavi Salman
__license__  = Public Domain
__version__  = 1.0
'''

# Input 

# Take String Value as Input from User and store in String Variable fruit_of_jannah

fruit_of_jannah = input("Name the Fruit of Jannah? ")

# Processing + Output

# Display Values stored in fruit_of_jannah

print(fruit_of_jannah, "is the fruit of jannah")

# Display Data Types of fruit_of_jannah

print("Data Type of String Variable are: ", type(fruit_of_jannah))

# Display Memory Addresses of fruit_of_jannah

print("Memory Address of String Variable is: ", id(fruit_of_jannah))
				
			
				
					Name the Fruit of Jannah? Anjeer
Anjeer is the fruit of jannah
Data Type of String Variable are:  <class 'str'>
Memory Address of String Variable is:  139731417268496
				
			
Analysis, Main Findings and Conclusions
Analysis of Python Program
  • In this Python Program, I displayed the Value, Data Type and Memory Address of String Variable on the Output Screen using 
    • print() Function
    • type() Function
    • id() Function
Main Findings
  • Alhamdulillah (الحمدللہ) 😊, print() Function efficiently and effectively performed my required Task i.e., 
    • Displaying Value, Data Type and Memory Address of String Variable on the Output Screen
Conclusions
  • To conclude, our main goal i.e.
    • Displaying Value, Data Type and Memory Address of String Variable on the Output Screen was successfully accomplished using print(), type() and id() Functions. Alhamdulillah (الحمدللہ) 😊
  • Task 2
Completely and Correctly Understand the Task
Task Description
  • Write a Program in Python which stores String Values (i.e., Hazrat Abu Bakar Siddique (R.A.) and Hazrat Amma Khadija (R.A.)) in a String Variables (at Run Time) and display the Values, Data Types and Memory Addresses of a String Variables on the Output Screen. Note that the name of String Variables should be male_sahabi and female_sahabi.
Sample Output

Who is the First Male Sahabi to accept Islam? Hazrat Abu Bakar Siddique (R.A.)

Who is the First Female Sahabi to accept Islam? Hazrat Amma Khadija (R.A.)

Hazrat Abu Bakar Siddique (R.A.) was the first Male Sahabi to accept Islam and Hazrat Amma Khadija (R.A.) was the first Female Sahabi to accept Islam

Data Type of String Variables are:  <class ‘str’> and <class ‘str’>

Memory Addresses of String Variables is:  139731417253384 and 139731526932960

Plan and Design Solution to the Task
Input-Processing-Output
  • Input
    • Data
      • Two String Values (i.e., Hazrat Abu Bakar Siddique (R.A.) and Hazrat Amma Khadija (R.A.)) stored in String Variables (male_sahabi and female_sahabi)
    • Instruction(s)
      • Display Values, Data Types and Memory Addresses of the Variables (male_sahabi and female_sahabi) on the Output Screen 
  • Output
    • Values, Data Types and Memory Addresses of the Variables (male_sahabi and female_sahabi) displayed on the Output Screen 
  • Processing
    • Use print() Function – To display String Values (stored in the Variables) on the Output Screen
    • Use type() Function – To Display Data Types of Variables on the Output Screen
    • Use id() Function – To Display Memory Addresses of Variables on the Output Screen
Implementation in Python
Code
				
					'''
__author__   = Ms. Samavi Salman
__copyright__= Copyright (C) 2020 Ms. Samavi Salman
__license__  = Public Domain
__version__  = 1.0
'''

# Input 

# Take String Values as Input from User and store in String Variables (Hazrat Abu Bakar Siddique (R.A.) and Hazrat Amma Khadija (R.A.))

male_sahabi = input("Who is the First Male Sahabi to accept Islam? ")
female_sahabi = input("Who is the First Female Sahabi to accept Islam? ")

# Processing + Output

# Display Values stored in Hazrat Abu Bakar Siddique (R.A.) and Hazrat Amma Khadija (R.A.)

print(male_sahabi + " was the first Male Sahabi to accept Islam and " + female_sahabi + " was the first Female Sahabi to accept Islam")

# Display Data Types of Hazrat Abu Bakar Siddique (R.A.) and Hazrat Amma Khadija (R.A.)

print("Data Type of String Variables are: ", type(male_sahabi),"and",type(female_sahabi))

# Display Memory Addresses of Hazrat Abu Bakar Siddique (R.A.) and Hazrat Amma Khadija (R.A.)

print("Memory Addresses of String Variables is: ", id(male_sahabi),"and",id(female_sahabi)) 
				
			
				
					Who is the First Male Sahabi to accept Islam? Hazrat Abu Bakar Siddique (R.A.)
Who is the First Female Sahabi to accept Islam? Hazrat Amma Khadija (R.A.)
Hazrat Abu Bakar Siddique (R.A.) was the first Male Sahabi to accept Islam and Hazrat Amma Khadija (R.A.) was the first Female Sahabi to accept Islam
Data Type of String Variables are:  <class 'str'> and <class 'str'>
Memory Addresses of String Variables is:  139731417253384 and 139731526932960
				
			
Analysis, Main Findings and Conclusions
Analysis of Python Program
  • In this Python Program, I displayed the Values, Data Types and Memory Addresses of String Variables on the Output Screen using 
    • print() Function
    • type() Function
    • id() Function
Main Findings
  • Alhamdulillah (الحمدللہ) 😊, print() Function efficiently and effectively performed my required Task i.e.,
    • Displaying Values, Data Types and Memory Addresses of String Variables on the Output Screen
Conclusions
  • To conclude, our main goal i.e.
    • Displaying Values, Data Types and Memory Addresses of String Variables on the Output Screen was successfully accomplished using print(), type() and id() Functions. Alhamdulillah (الحمدللہ) 😊

TODO and Your Turn

Todo Tasks
Your Turn Tasks
Todo Tasks

TODO Task 1

  • Task 
    • Consider the following Tasks and answer the questions given below
  • Note 
    • Your answer should be 
      • Well Justified
  • Questions
    • Write Python Programs for all the Tasks given below by following the Template given in this Chapter i.e., 
      • Completely and Correctly Understand the Task
        • Task Description
        • Sample Output
      • Plan and Design Solution of the Task
        • Input-Processing-Output
          • Input 
            • Data
            • Instructions
          • Output
          • Processing
      • Implementation in Python
        • Code
        • Output of Code
      • Analysis, Main Findings and Conclusions
        • Analysis of Python Program
        • Main Findings
        • Conclusions
  •  
    •  
      •  
        •  
  • Note
    • The Memory Addresses uses in the Sample Output are Dummy Values

 

                        Task  1

Completely and Correctly Understand the Task

Task Description

  • Write a Program in Python which stores Integer Value (i.e., 30) in an Integer Variable (at Run Time) and displays the Value, Data Type and Memory Address of an Integer Variable on the Output Screen. Note that the name of Integer Variable should be paras_in_quran.

Sample Output

How many Paras are there in Holy Quran? 30

There are 30 Paras in Quran.e.Pak 

Data Type of Integer Variable: <class ‘int’>

Memory Address of Integer Variable is: 9079200

 

                        Task  2

Completely and Correctly Understand the Task

Task Description

  • Write a Program in Python which stores Integer Values (i.e., 86 and 28) in an Integer Variables (at Run Time) and displays the Values, Data Types and Memory Addresses of an Integer Variables on the Output Screen. Note that the name of Integers Variables should be makki_surahs and madani surahs.

Sample Output

How many Makki Surahs are there in Holy Quran? 86

How many Madani Surahs are there in Holy Quran? 28

There are 86 Makki and 28 Madani Surah in Quran.e.Pak 

Data Type of Integer Variables: <class ‘int’>

Memory Addresses of Integer Variables are: 9099200

 

                        Task  3

Completely and Correctly Understand the Task

Task Description

  • Write a Program in Python which stores Float Value (i.e., 15.0) in a Float Variable (at Run Time) and displays the Value, Data Type and Memory Address of a Float Variable on the Output Screen. Note that the name of Float Variable should be sajdas_in_quran.

Sample Output

How many Sajdas are there in Holy Quran? 15.0

There are 15.0 Sajdas in Quran.e.Pak 

Data Type of Float Variable: <class ‘float’>

Memory Address of Float Variable is: 9179200

 

                        Task  4

Completely and Correctly Understand the Task

Task Description

  • Write a Program in Python which stores Float Values (i.e., 25.0 and 40.0) in a Float Variables (at Run Time) and displays the Values, Data Types and Memory Addresses of a Float Variables on the Output Screen. Note that the name of Float Variables should be age_of_prophet and age_of_amma.

Sample Output

What was the age of Holy Prophet (S.A.W.W.) at the time of marriage? 25.0

What was the age of Amma Khadija (R.A.) at the time of marriage? 40.0

At the time of first marriage, Hazrat Muhammad S.A.W.W. was 25.0 years old and Hazrat Amma Khadija R.A. was 40.0 years old

Data Type of Float Variables: <class ‘float’> and <class ‘float’>

Memory Addresses of Float Variables are: 9279200 and 9279202

 

                        Task  5

Completely and Correctly Understand the Task

Task Description

  • Write a Program in Python which stores String Value (i.e., Hazrat Fatima R.A.) in a String Variable (at Run Time) and displays the Value, Data Type and Memory Address of a String Variable on the Output Screen. Note that the name of String Variable should be role_model.

Sample Output

Who is role model for all the Muslim women? Hazrat Fatima R.A.

Hazrat Fatima R.A. is role model for all the Muslim women

Data Type of String Variable: <class ‘string’> 

Memory Address of String Variable are: 8279200

 

                        Task  6

Completely and Correctly Understand the Task

Task Description

  • Write a Program in Python which stores String Values (i.e., Hazrat Imam Hassan R.A. and Hazrat Imam Hussain R.A.) in a String Variables (at Run Time) and displays the Values, Data Types and Memory Addresses of a String Variables on the Output Screen. Note that the name of String Variables should be name_of_first_son and name_of_second_son.

Sample Output

What is the name of first son of Hazrat Fatima R.A.? Hazrat Imam Hassan R.A. 

What is the name of second son of Hazrat Fatima R.A.? Hazrat Imam Hussain R.A.

Hazrat Imam Hassan R.A. and Hazrat Imam Hussain R.A. are two children of Hazrat Fatima R.A. 

Data Type of String Variables: <class ‘string’> and <class ‘string’>

Memory Addresses of String Variables are: 1292022 and 1292021

 

Your Turn Tasks

Your Turn Task 1

  • Task
    • Write at least 6 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.,
  • Note 
    • Your answer should be 
      • Well Justified
  • Questions
    • Write Python Programs for all the Tasks given below by following the Template given in this Chapter i.e., 
      • Completely and Correctly Understand the Task
        • Task Description
        • Sample Output
      • Plan and Design Solution of the Task
        • Input-Processing-Output
          • Input 
            • Data
            • Instructions
          • Output
          • Processing
      • Implementation in Python
        • Code
        • Output of Code
      • Analysis, Main Findings and Conclusions
        • Analysis of Python Program
        • Main Findings
        • Conclusions
  •  
    •  
      •  
        •  
  • Note
    • The Memory Addresses uses in the Sample Output are Dummy Values

Chapter Summary

  • Chapter Summary

In this Chapter, I presented the following main concepts:

  • Variable
    • A Variable is defined as the Name Assigned to a Memory Location / Memory Address (to store Data (Values / Constants)), that can change (i.e., does not remain the same) every time a Python Program is executed
  • Three main Components of a Variable are
    • Variable Name
    • Value (stored) in a Variable (i.e., Content of a Variable)
    • Memory Location / Memory Address (used to store Value of a Variable)
  • Main Operations on a Variable
    • The main Operations performed on a Variable are
  • Operation 01
    • Store Value in a Variable
  • Operation 02
    • Modify / Update Value of a Variable
  • Operation 03
    • Use Value of a Variable for various Tasks
  • In Python, there are three main Types of Variables
    • Integer Variables
    • Float Variables
    • String Variables
  • Integer Variable
    • An Integer Variable is defined as the Name Assigned to a Memory Location / Memory Address (to store Integer Values (Constants)), that can change (i.e., does not remain the same) every time a Python Program is executed
  • Float Variable
    • A Float Variable is defined as the Name Assigned to a Memory Location / Memory Address (to store Floating-Point Values (Constants)), that can change (i.e., does not remain the same) every time a Python Program is executed
  • String Variable
    • A String Variable is defined as the Name Assigned to a Memory Location / Memory Address (to store Fixed Sequence of Characters (Constants)), that can change (i.e., does not remain the same) every time a Python Program is executed
  • input () Function
    • The input() Function takes User input (reads a Line) convert it in a String and return it
  • Code Time Variable Value vs Run Time Variable Value
    • Code Time Variable Value
      • At Code Time, the Value to be stored in a Variable is
        • Decided by the Programmer
    • Run Time Variable Value
      • At Run Time, the Value to be stored in a Variable is
        • Decided by the User (of the Program / Software)
  • Dry Run
    • The testing process which reduces the effect of Failure is called Dry Run
      • Check Data Type of Variable using type() Function
      • Check Memory Address of a Variable using id() Function
  • Variable Naming Conventions – Best Coding Practices
    • Best Coding Practice 01
      • Variable Name should be in lower case
    • Best Coding Practices 02
      • Use expressive Variable Names
    • Best Coding Practices 03
      • Use underscore to separate words in long Variable Names

In Next Chapter

  • In Next Chapter
  • In Sha Allah, in the next Chapter, I will present a detailed discussion on
  • o Operators and Expressions in Python
Chapter 5 - Constants in Python
  • Previous
Chapter 7 - Operators and Expressions
  • Next
Share this article!
Facebook
Twitter
LinkedIn

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

About Us

Ilm O Irfan Technologies believes that honest dedicated hard work with the ethical approach of commitment true to every word, and trust-building is the road to success in any field of business or life.

Quick Links
  • About Us
  • Contact
Useful Links
  • Privacy Policy
  • Terms and Conditions
  • Disclaimer
  • Support
  • FAQ
Subscribe Our Newsletter
Facebook Twitter Linkedin Instagram Youtube

© 2022 Ilmo Irfan. All Rights Reserved.