Programming Concepts
(Python)
Computer Science (0478) - MAA - Aitchison College, Lahore.
A named data storage area that contain a value that may change during the execution of a program.
Every variable name (called identifier) should be meaningful that remind you of what data the variable
stores. identifier value (literals)
number = 10
Variables and Constants
A named data storage area than contains a value that does not change during the execution of a
program.
As with variables, in order to make programs understandable to others, constants should also be given
meaningful names with the keyword “CONST” or “CONSTANT”.
Example:
Computer Science (0478) - MAA - Aitchison College, Lahore. 2
Python Keywords and Identifiers
Keywords are predefined, reserved words used in Python programming that have special meanings to the
compiler.
We cannot use a keyword as a variable name, function name, or any other identifier.
Computer Science (0478) - MAA - Aitchison College, Lahore. 3
Python Keywords and Identifiers
Identifiers are the name given to variables, classes, methods, etc.
For example:
Here, language is a variable (an identifier) which holds the value 'Python'.
Rules for naming an identifier:
Identifiers cannot be a keyword.
Identifiers are case-sensitive.
It can have a sequence of letters and digits. However, it must begin with a letter or _.
The first letter of an identifier cannot be a digit.
It's a convention to start an identifier with a letter rather _.
Whitespaces are not allowed.
We cannot use special symbols like !, @, #, $, and so on.
ComputerScience
Computer Science (0478)
(0478) --MAA
MAA- Aitchison College,
- Aitchison Lahore.
College, Lahore. 4
Basic data types in programming
You will need to tell your program what type of data you want it to store in a variable.
In some programming languages declaration of data is done when you first use it. (Using DECLARE
keyword)
Datatype Description Pseudocode Python Example
Text – characters, numbers and
symbols
String STRING str “hello”, ‘1Af2’
The data will always need to be inside
speech marks.
Integer Whole numbers. INTEGER int 23, -300, 4565
Real Decimal numbers. REAL float 1.2, -20.49
One character or number of symbol.
Char The data will always need to be inside CHAR str “h”, ‘f’, “9”, ‘?’
speech marks.
Boolean Either true or false. BOOLEAN bool TRUE, FALSE
Computer Science (0478) - MAA - Aitchison College, Lahore. 5
Difference between Assignment operator and Equality
= ==
It is an assignment operator It is comparison or relational operator
It is used to assigning the value to a It is used for comparing two values
variable Constant term can be placed in the
Constant term cannot be placed on left hand side
left hand side Example:
Example: 1 = = x; is valid
1 = x; is invalid
Computer Science (0478) - MAA - Aitchison College, Lahore. 6
Input statements in programming
Taking inputs from the user:
Python Code:
Number = int ( input (“Enter a number:”))
Syntax:
Variable = datatype ( input ( Prompt statement))
• For a program to be useful, the user needs to know what they are expected to input, so each input needs to
be accompanied by a prompt stating the input required.
Output will be:
Enter a number:
Computer Science (0478) - MAA - Aitchison College, Lahore. 7
Output statements in programming
Displaying data:
• For a program to be useful, the user needs to know what results are being output, so each output needs to
be accompanied by a message explaining the result.
• To do this, the keyword word print is used.
Example: 1 Cost = 22.54 Example: 2 print (“ Pakistan ” )
print (“ The cost is ” , Cost )
• If there are several parts to an output statement, then each part is separated by a separator character
called comma ( , ).
Output will be:
The costs is 22.54 Pakistan
Computer Science (0478) - MAA - Aitchison College, Lahore. 8
Comments in programming / pseudocode
In programming , comments are written to state what a particular line or section of code is for.
They are used by programmers to add explanatory notes for anyone who looks at the program
and wants to understand it.
These are lines of code in the programming that the computer will ignore during execution.
A commented lines in programming starts with a hash (#) or triple quotes (“ “ “ ) symbol.
For example:
# this is a comment!
# this variable holds the player’s score
# your name, center number, school number
Computer Science (0478) - MAA - Aitchison College, Lahore. 9
Arithmetic, Logical and Boolean operators in programming
Arithmetic operators:
All programming languages make use of arithmetic operators to perform calculations.
Operators in Pseudocode Action Python
+ Add +
- Subtract -
* Multiply *
/ Divide /
^ Raise to the power of **
MOD Remainder division %
DIV Integer division //
Computer Science (0478) - MAA - Aitchison College, Lahore. 10
Arithmetic, Logical and Boolean operators in programming
Logical operators:
All programming languages make use of logical operators to decide which path to take through a program.
Operators in Pseudocode Comparison Python
> Greater than >
< Less than <
= Equal ==
>= Greater than or equal >=
<= Less than or equal <=
<> Not equal !=
Computer Science (0478) - MAA - Aitchison College, Lahore. 11
Arithmetic, Logical and Boolean operators in programming
Boolean operators:
All programming languages make use of Boolean operators to decide whether an expression is true or false.
Operators in Pseudocode Description Python
AND Both True and
OR Either True or
NOT Not True not
Computer Science (0478) - MAA - Aitchison College, Lahore. 12
Q:1 Write a program that will input two numbers and display their sum.
number1 = int (input ("Enter first number"))
number2 = int (input ("Enter second number"))
Add = number1 + number2
print (“The sum of two numbers: ”, Add)
Q: 2 Write a program that calculates and displays the volume of a cylinder.
Hint: Volume = Radius * Radius * Length * Pi
Constant Pi = 3.14
Radius = int (input ("Enter the radius of a cylinder: "))
Length = int (input ("Enter the length of a cylinder"))
Volume = Radius * Radius * Length * Pi
print (“The volume of a cylinder is ”, Volume)
Computer Science (0478) - MAA - Aitchison College, Lahore. 13
Homework:
Q:3 Write a program that input base and height of a triangle and output the area of a triangle.
Solution:
(Area=1⁄2*base*height)
b a s e =float(input ("Enter the base value"))
h e i g h t = float (input ("Enter height of a triangle"))
Area = 0.5 * b a s e * h e i g h t
print (“The area of two a triangle: ”, Area)
Q:4 Write a program that input a number from user and output its square value.
Solution:
N u m = int(input ("Enter the value"))
Sq = N u m * N u m
print (“The square is ”, Sq)
Computer Science (0478) - MAA - Aitchison College, Lahore. 14
Selection statements in programming
A program to test several conditions, and execute instructions based on which condition is true.
IF statement Syntax in Python Language
if logical test:
execute statements if test is true.
else:
execute statements if test is false.
Example: Write a program code that compares two values, if the values are same it outputs “ That is correct ”. If
they are not the same then it will output “ Incorrect entry ”.
Num = 10
Guess = int ( input (“ Enter a number ” ))
if Num == Guess :
print( “ That is correct ”)
else:
print( “ Incorrect entry ” )
Computer Science (0478) - MAA - Aitchison College, Lahore. 15
Exercise:
Q: Write a program code to sell tickets for a ride on the Nightmare Roller Coaster. If you are younger than 15
years old you cannot buy a ticket and will output the message “Sorry come back when you are older.” and if
you are older or equals to 15 years old, the program will output the message “You can buy a ticket at your
own risk.”
print(“Buy a ticket for the Nightmare Roller Coaster. You must be 15 or over”)
Age = int(input(“ Please enter your age: ”))
If Age < 15 :
print(“Sorry come back when you are older.”)
else :
print(“You can buy a ticket at your own risk.”)
Computer Science (0478) - MAA - Aitchison College, Lahore. 16
Homework:
Q: Write a python code that input radius of circle from user and output its area by using the
formula: p i * r * r
Constant Pi = 3.14
Radius = int (input ("Enter the radius of a circle: "))
Area = Pi * Radius * Radius
print (“The Area of a circle is ”, Area)
Computer Science (0478) - MAA - Aitchison College, Lahore. 17
Conditional / Selection Statements
If...else
Used when algorithm has two paths or outcomes
If....elif...else
Used when algorithm has multiple outcomes (more than 2)
Computer Science (0478) - MAA - Aitchison College, Lahore. 18
Make use of two or more IF statements, the second or other ‘IF’ statements are part of
Nested IF the ‘ELSE’ path.
Example: A rejected percentage mark must be either less
than zero or greater than 100.
PercentageMark = int ( input ( “ Please enter a mark” ))
if PercentageMark < 0 or PercentageMark > 100 :
print( “ Invalid mark ”)
elif PercentageMark > 49 :
print ( “ Pass” )
else : This is a nested IF statement, shown clearly by
print ( “ Fail” ) the use of second level of indentation. The
percentage mark is only tested if it is in the
correct range.
Computer Science (0478) - MAA - Aitchison College, Lahore. 19
Exercise:
Q: Write a python code to check for a mark between 0 and 20 and a pass mark of 10.
Python code:
Mark = int ( input ( “ Enter a mark between 0 and 20: ” ))
if Mark < 0 or Mark > 20 :
print ( “ Invalid mark” )
elif Mark > = 10 :
print ( “ Pass” )
else :
print ( “ Fail” )
Computer Science (0478) - MAA - Aitchison College, Lahore. 20
Conditional / Selection Statements
If...else
Used when algorithm has two paths or outcomes
If....elif...else
Used when algorithm has multiple outcomes (more than 2)
Computer Science (0478) - MAA - Aitchison College, Lahore. 21
Example: Write a python code that input one of the four arithmetic operators and then carries out the
calculations respectively.
Python uses elif for multiple tests
In Python Language
Operator = str(input(“ Choose the suitable operator sign ( +, -, *, / ) ” ))
if Operator == “+”:
Answer = Number1 + Number2
elif Operator == “-”:
Answer = Number1 – Number2
elif Operator == “*”:
Answer = Number1 * Number2
elif Operator == “/”:
Answer = Number1 / Number2
else: print(“Invalid operator”)
print(“The answer is: ”, Answer )
Computer Science (0478) - MAA - Aitchison College, Lahore. 22
Q: Write a python code by using elif statement to display the day of the week if the variable ‘Day’ has a whole
number value between 1 and 7 inclusive and an error message is displayed otherwise.
Day = int (input“ Choose the day of the week from 1 to 7 : ” ))
if Day == 1:
print (“Sunday”)
elif Day == 2:
print (“Monday”)
elif Day == 3: elif Day == 6:
print (“Tuesday”) print (“Friday”)
elif Day == 4: elif Day == 7:
print (“Wednesday”) print (“Saturday”)
elif Day == 5: else:
print (“Thursday”) print( “Error: You didn’t choose a valid option from 1 to 7. ” )
Computer Science (0478) - MAA - Aitchison College, Lahore. 23
Exercise:
Q: Take integer values in variable and write a code to swap the values.
Python code:
Value1, Value2 = int ( input ( “ Enter two values: ” ))
Temp = Value1
Value1 = Value2
Value2 = Temp
# Values after swapping are
print (“Value1 is now”, Value1)
print (“Value2 is now”, Value2)
Computer Science (0478) - MAA - Aitchison College, Lahore. 24
Q: Write a python code that input a number from user and print if its even or odd.
Computer Science (0478) - MAA - Aitchison College, Lahore. 25
Q: Write a python code that input three numbers from user and output the largest number
input.
Computer Science (0478) - MAA - Aitchison College, Lahore. 26
Q: Write a python code that input a number from user and output if its positive negative or
zero.
Computer Science (0478) - MAA - Aitchison College, Lahore. 27
Q: Write a python code to output a grade A to D for a class test if the variable Score has input the following
values from the user. Print Grade A if Score is > = 80, Grade B if Score is > = 70, Grade C if Score is > = 60,
Grade D if Score is > = 50, otherwise print Grade U if score is less than 50.
Score = int(input(“Enter the score in the class test ” ))
if Score >= 80 :
print(“ A ”)
elif Score >= 70 :
print(“ B ”)
elif Score >= 60 :
print(“ C ”)
elif Score >= 50 :
print(“ D ”)
else:
print(“ U ”)
Computer Science (0478) - MAA - Aitchison College, Lahore. 28
Exercise:
Write a program using pseudocode or program code to solve the problem.
A program takes a number from the user. It then counts from 1 to the number that the user has entered
(inclusive).
If the number being input is divisible by 5, it outputs a message.
If the number being input is divisible by 7, it outputs a message.
Computer Science (0478) - MAA - Aitchison College, Lahore. 29
Solution:
• A program takes a number from the user. It then counts
from 1 to the number that the user has entered
(inclusive).
If the number being input is divisible by 5, it outputs a
message.
If the number being input is divisible by 7, it outputs a
message.
Computer Science (0478) - MAA - Aitchison College, Lahore. 30