0% found this document useful (0 votes)
45 views66 pages

Presentation 4 - Inputs and Sequence

Here are the steps to solve these problems in sequence: Q1) 1. Declare constants PI, add, sub, mul, div 2. Take input num1, num2 as integers 3. Process: result_add = num1 + num2 result_sub = num1 - num2 result_mul = num1 * num2 result_div = num1 / num2 4. Print results Q2) 1. Declare variables length, breadth 2. Take inputs length, breadth as floats 3. Calculate area = length * breadth 4. Print area The key is to break down the problem into clear steps, declare

Uploaded by

rayanahmed00123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views66 pages

Presentation 4 - Inputs and Sequence

Here are the steps to solve these problems in sequence: Q1) 1. Declare constants PI, add, sub, mul, div 2. Take input num1, num2 as integers 3. Process: result_add = num1 + num2 result_sub = num1 - num2 result_mul = num1 * num2 result_div = num1 / num2 4. Print results Q2) 1. Declare variables length, breadth 2. Take inputs length, breadth as floats 3. Calculate area = length * breadth 4. Print area The key is to break down the problem into clear steps, declare

Uploaded by

rayanahmed00123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 66

Python – Inputs & Sequence

Seating arrangement
Yellow notebooks
Lesson Objectives Quiz on 29th Sept

2.4.1 Understand how to write code that accepts


and responds appropriately to user input.

Demonstrate the ability to write Python programs


using correct input statements
How to take inputs in Python ??
+Integer inputs
+Float inputs
+String/Character inputs – same method
+Boolean inputs
By default, the input function
stores the value as STRING
So, if you need to use the value entered as an
integer , float or Boolean then you need to convert it
Integer inputs in Python
Seating arrangement
Yellow notebooks
Lesson Objectives Quiz on 29th Sept

2.4.1 Understand how to write code that accepts


and responds appropriately to user input.

Demonstrate the ability to write Python programs


using correct input statements
Float inputs in Python
String/Character inputs in Python
Boolean inputs in Python
Boolean inputs in Python
Using the type function in Python
Coding CW
You want a student to enter the following details in a program

1) Full name

2) Age in whole numbers

3) Address

4) Contact Number

5) Whether he/she would like to opt for extra reinforcement classes or not

6) Current class section – class sections are single letters like A,B,C etc

7) Marks in the latest exam – Could be in decimals

USING THE SCRIPT MODE OF PYTHON , ASK FOR THE ABOVE INPUTS AND DISPLAY ALL THE ANSWERS. UPLOAD ON
SHOWBIE UNDER CW 5 – INPUTS IN PYTHON
Quiz#2
Please submit the
python file for Q3
on Showbie under
Quiz # 2
Test on Thursday 6th

Lesson Objectives

Feedback for Quiz # 2

Demonstrate the ability to write Python programs


using input, output, operators, variables and data
types
Python Code Snippets
Taking Input in Python
Q) a) Take the name and age of a student as input and store the inputs in appropriate
variables
b) What will be the age of the student after 10 years?
Let’s take the name as input first. Which one is the correct way to take the
input? And why?

A. name=int(input(“Enter your name: “))

A. name=input(“Enter your name:”))

A. name=float(input(“Enter your name:”))


Now let’s take the age as input. Which one is the correct way to take the
input? And why?

A. age=int(input(“Enter your age: “))

A. 123_age=input(“Enter your age:”))

A. *%_name=float(input(“Enter your age:”))

A. studentage=chr(input(“Enter your age:”))


b) What will be the age of the student after 10 years?

age=age+10

age_10=age+10

final_age=age+10

age_after_10_years=age+100
Producing Outputs in Python
Q) The following data about a student has been input:

student_name=”Esha”
age=16
address=”House No 34/1, DHA Phase 6, Karachi”
contact_number=033456784
promoted_class_11=True
school_fees_paid=’Y’
overall_percentage_class_10=89.9

Check the print statements on the next slides and determine which ones
are correct and which ones are not and explain why.
student_name=”Esha”
age=16
address=”House No 34/1, DHA Phase 6, Karachi”
contact_number=033456784
promoted_class_11=True
school_fees_paid=’Y’
overall_percentage_class_10=89.9
A. PRINT(“The name is “, name)

A. print(“The student: “, student_name, “is “, age, “years old”)

A. Print( The Grade 10 overall % is”, overall)

A. print(“The contact number of the student is” contact_number)

A. pRint(“The address of the student is, address)


Data Types in Python
Which inputs have been taken incorrectly? And why?
1. student_name=chr(input(“Enter your name: “))

2. age=bool(input(“ Enter your age“))

3. address=int(input(“ Enter your address:“))

4. contact_number=str(input(“Enter your contact number “))

5. promoted_class_11=bool(int((input(“Enter True for yes and False for no“)))

6. school_fees_paid=int(input(“Enter Y for yes and N for no “))

7. overall_percentage_class_10=int(input(“Enter the percentage “))


Variables in Python

Are these variable names valid?

A. 1_name
B. name_&_
C. _age_
D. _address_123
E. !_weight
F. Height_4
G. average_*
3 Programming Constructs/Concepts
1. Sequence
2. Selection
3. Iteration
3 important concepts of programming

When you write lines of code, there are three ways you can control the order these lines will be executed
by the computer:

1. Sequencing: This means that the computer will run your code in order, one line at a time from the
top to the bottom of your program. It will start at line 1, then execute line 2 then line 3 and so on
till it reaches the last line of your program.
2. Selection: Sometimes you only want some lines of code to be run only if a condition is met,
otherwise you want the computer to ignore these lines and jump over them. This is achieved using
IF statements. e.g. If a condition is met then lines 4, 5, 6 are executed otherwise the computer
jumps to line 7 without even looking at line 4,5 and 6.
3. Iteration: Sometimes you want the computer to execute the same lines of code several times. This
is done using a loop. There are three types of loops: For loops, while loops and repeat until loops.
That’s handy as it enables you not to have to copy the same lines of code many times.
SEQUENCE
What is sequence?
(CLICK on the link attached)
Sequencing in Algorithms

An algorithm is a plan, a set of step-by-step instructions to solve a problem. There are


three basic building blocks (constructs) to use when designing algorithms:

● sequencing
● selection
● iteration

These building blocks help to describe solutions in a form ready for programming.
Algorithms consist of instructions that are carried out (performed) one after another.

Sequencing is the specific order in which instructions are performed in an algorithm.


Why is sequencing important?
For example, a very simple algorithm for
brushing teeth might consist of these steps: It is crucial that the steps in an algorithm are performed
in the right order - otherwise the algorithm will not
1. put toothpaste on toothbrush work correctly. Suppose the steps for the teeth-cleaning
2. use toothbrush to clean teeth algorithm were in this sequence:

3. rinse toothbrush 1. use toothbrush to clean teeth


2. put toothpaste on toothbrush
3. rinse toothbrush
Each step is an instruction to be performed.
Sequencing is the order in which the steps are
A toothbrush would still be used to clean the teeth and
carried out.
toothpaste would still be put on the brush. But because
A computer can only do what it is steps 1 and 2 are in the wrong sequence the teeth
wouldn’t get cleaned with the toothpaste, and the
programmed to do. If the steps are toothpaste would be wasted.
programmed in the wrong sequence, the
A human would realise they had forgotten to add
computer will perform the tasks in this
toothpaste at the start of the process, but a computer
sequence – even if this is incorrect. would not know that anything was wrong.
Sequencing in practice: Drawing a square
An algorithm to get a computer to draw a square on the screen might consist of
these steps:

1. draw a 3 cm line
2. turn left 90 degrees
3. draw a 3 cm line
4. turn left 90 degrees
5. draw a 3 cm line
6. turn left 90 degrees
7. draw a 3 cm line

The steps in this algorithm are in the correct sequence. This algorithm would result
in a perfect square as shown in the picture
If there was a mistake when designing the algorithm, and the steps in this sequence were
placed like this: Because step 6 is in the
1. draw a 3 cm line wrong sequence (it should
switch with step 7), this
2. turn left 90 degrees
algorithm failed. However,
3. draw a 3 cm line
fixing the algorithm - and
4. turn left 90 degrees the square - is easy, because
5. draw a 3 cm line there are only seven steps
6. draw a 3 cm line to look through for the
7. turn left 90 degrees error.

This algorithm would create this shape, rather than a perfect square.
Complex algorithms may have hundreds, if not
thousands, of steps.

It is critical to make sure all steps in the algorithm are in


the correct sequence before programming begins.

Once programmed, trying to find an instruction in the


wrong sequence can be extremely difficult.
In sequence questions:
1. Declare the constants and variables(specified)-
Make sure the names are valid
2. Take the correct inputs and store them
variables/constants using the correct data types
3. Process the inputs
4. Print the outputs
Practice Questions: Sequence
Q1) Write a program to enter two numbers and perform all arithmetic operations such as
addition, multiplication, subtraction and division.

Q2) Write a program to enter length and breadth of a rectangle and find its perimeter.
(P=2(L) + 2(B))

Q3) Write a program to enter length and breadth of a rectangle and find its area.

Q4) Write a program to enter radius of a circle and find its diameter, circumference and
area.

Q5) Write a program to enter length in centimeter and convert it into meter and kilometer.

Q6) Write a program to enter temperature in Celsius and convert it into Fahrenheit. To
convert temperatures in degrees Celsius to Fahrenheit, multiply by 1.8 (or 9/5) and add 32.
Q7) Write a program to enter temperature in Fahrenheit and convert to Celsius

Q8) Write a program to convert days into years, weeks and months.

Q9) Write a program to find power of any number x ^ y.

Q10) Write a program to enter any number and calculate its square root.

Q11) Write a program to enter two angles of a triangle and find the third angle.

Q12) Write a program to enter base and height of a triangle and find its area.

Q13) Write a program to calculate area of an equilateral triangle.(Area = ((3**0.5)/4)*(a**2)

Q14) Write a program to enter marks of five subjects and calculate total, average and
percentages of each subject as well as the overall percentage.
Q15) Write a program that solves a quadratic equation.

Q16) Write a program that prints ‘Hello World’ to the screen.

Q17) Write a program that asks the user for their name and greets them with their name.

Q18) Calculate the compound interest on a given principal amount, time period and rate
of interest. The formula is

Q19) Calculate the simple interest on a given principal amount, time period and rate
of interest. The formula is SI=PRT/100

Q20) Use the Pythagorean theorem to calculate the hypotenuse of a right angled
triangle (c**2=a**2 + b**2)

Q21) Calculate the perimeter of a square


Q22) Find the volume and surface area of a sphere. (SA=4*Pi*r2) (V=(4/3)*Pi*r3)

Q23) Find the volume and surface area of a cone. (SA=Pi*r(r+s)) (V=(1/3)*Pi*r2*h)

Q24) Write a program that reads a number in inches, converts it to meters. Note: One inch is
0.0254 meters

Q25) Input a 2 digit number and print the sum of the 2 digits in the number for example if 78 is
entered, 15 is output (Hint: Use the quotient and remainder theorem)

Q26) Write a program to convert minutes into a number of years and days

Q27) Write a program which takes the distance and time and outputs the speed. (S=D/T)

Q28) Write the program which takes 2 numbers as inputs, a and b, and prints the
remainder when a is divided by b and vice versa
Q29) Write a program in which 2 values are input into 2 different variables and then the
values are swapped and output.

Q30) Find the volume and surface area of a cuboid. (V=L*B*H) (SA=LW+WH+HL)

Q31) Find the volume and surface area of a cylinder. (V=Pi*r2*h) (SA=Pi*2*r(r+h))

Q32) Find the area and perimeter of a parallelogram. (A=b*h)(P=2(a+b))

Q33) Find the area and perimeter of a rhombus (Area=pq/2 where p and q are diagonals, P=4a
where a is the side) and a trapezoid (A=(a+b/2)*h where a and b are bases, h is the height,
P=a+b+c+d which are all sides)

Q34) Write a program to add 3 numbers and print the output

Q35) Write a program which takes the name, age, contact number, address and DOB of a
student and stores all the details. Print the details back for the student to check and verify.
Q36) Write a program which calculates the cube root of an integer

Q37) Write a program which outputs the quotient only after a number is divided by another
number

Q38) Write a program which outputs the remainder only after a number is divided by
another number

Q39) Write a program which outputs the result of the complete division when a number is
divided by another number
Sequence Q1
Maria decides to buy some cupcakes and brownies.

One brownie costs $4.5 and one cupcake costs $5.5.


She decides to buy 15 cupcakes and 20 brownies.
#Take the no of cupcakes and brownies as inputs #
The shopkeeper then gives her a 5% discount on her total bill.

Declare the number of cupcakes and brownies and output her


discount and the final bill.
Declare constants and variables with meaningful names.
Sequence Q2
Write a program which swaps 2 integers.
Sequence Q3
A school asks a teacher for the following when the teacher applies
for a vacancy:
1) Can you teach more than 2 subjects? (Answer can be a yes or
no)
2) Can you work overtime? (Answer can be a yes or no)
3) Can you take extra classes on Saturday? (Answer can be a yes
or no)
4) Are you interested in working for a society? (Answer can be a
yes or no)
5) Are you willing to take online courses for further professional
development? (Answer can be a yes or no)
Write the python code to take the 5 answers from one teacher and
then output the following using separate variables:

1) Is the teacher willing to teach more than 2 subjects and work


overtime?
2) Is the teacher willing to take professional development courses
or interested in working for a society?
3) Is the teacher willing to take extra classes on Saturdays or work
overtime?
4) Can you now print the values of all the variables for parts 1) 2)
and 3) using a not function?
Sequence Q4
John decides to go shopping to a supermarket. He intends
to buy detergent bottles, soaps, toothpastes and some
drinks.
Each detergent bottle costs $4.5, each soap costs $2 ,
each toothpaste costs $3.5 and each drink costs $8.
a) Calculate the total bill for John.
b) The supermarket has a 23% discount going on for all
customers. Print the discount John will receive and his
final bill.
Sequence Q5
A vendor decides to open a clothing store.
He purchases the following:

1) 55 t shirts each costing him $2


2) 100 pants each costing him $15
3) 90 jackets each costing him $50
Ask the vendor for how much does he want to sell each t shirt, pant
and jacket for and store the values in different variables. Then ask
the vendor the quantity sold for each type of item.
Print the total cost price and total selling price combined for all
items.
BONUS: Can you print
whether the vendor made a
profit or a loss by including
selection?
(He could also have broke
even)
Test # 1 – 25 marks
Please submit all files on Showbie after completion
Lesson Objectives

Demonstrate the ability to use constants in Python


programs

Introduction to selection using if statements


Important
1. Please check whether all work has been submitted or not – Showbie
2. All marks will be updated by tomorrow – Tuesday 11th October
3. Retest will be tomorrow in the 2nd break. Please reach the lab at 12:30
pm sharp
4. Please bring your notebooks and folders on Thursday 13th October –
Those who forget will lose marks.
5. Please login to your school email accounts and email all your work to
yourself – Use One Drive if necessary
6. Hackathon preparation – 17th Oct - 31st Oct – 1st Nov –14th Nov?
7. How many CWs have we done?
3 Programming Constructs/Concepts
1. Sequence- ?
2. Selection
3. Iteration
Constants in Python

How are constants declared in Python?

CONST_num_students=110

Whenever you think a value will


remain the same during the course
num_students=110 of a program use the word const in
the name like shown. This will tell
the person checking your code
that you consider it a constant not
a variable.
How to declare constants?

You might also like