SlideShare a Scribd company logo
1 of 30
Exercise: simple if
1
Ask the user to enter an integer
If the number is positive, print "Positive"
If the number is negative, say -10 print:
"Negative of 10"
If the number is zero, print "Zero"
Finally, print "Done"
2
3
4
5
Solution
x =int(input())
ifx < 0:
print("Negativeof",abs(x))
elifx > 0:
print("Positive")
else:
print("Zero")
print("Done")
Exercise: simple if 2
1 Ask the user to enter two integers, one on each
line say x and y
If x is larger than y print, "first", store the value of
x in a variable highest
If y is larger than x print, "second", store the value
of y in a variable highest
If they are equal, print "same", store either as the
highest
Finally, print the square of the highest
2
3
4
5
Solution
x =int(input())
y =int(input())
ifx > y:
print("first")
highest = x
elify > x:
print("second")
highest = y
else:
print("same")
highest = x
print(highest*highest)
Simple while loop 1
1
Use a while loop to print the numbers from 1 to 10
Solution
x = 1
whilex < 11:
print(x)
x += 1
Simple while loop 2
1 Use a while loop to print the odd numbers from 1
to 10
Solution
x = 1
whilex < 11:
print(x)
x += 2
Simple while loop 2
1 Use a while loop to generate the numbers from 1
to 10
If a number divisible by 3 is found, print ***
If a number divisible by 5 is found, print *****
Otherwise just print the number
2
3
4
Solution
x = 1
whilex < 11:
ifx%3 == 0:
print(’***’)
elifx%5 == 0:
print(’*****’)
else:
print(x)
x += 1
Simple while loop 3
1 Use a while loop to generate the numbers from 10
to 1
For each number, print that many * characters
That is, if the number is 1 print *, if the number is 3,
print *** etc.
2
3
Solution
x = 10
whilex > 0:
print(’*’*x)
x -= 1
Simple while loop 4
1 Ask the user for an integer, n (>=2)
Use a while loop to print n numbers uniformly
spaced between 0 and 1
Include both 0 and 1
2
3
For example if the user entered 3, print
0.0
0.5
1.0
Solution
n =int(input())
dx = 1.0/(n-1)
x = 0.0
whilex < (1.0-dx/2):
print(x)
x += dx
print(1.0)
Simple for loop 1
1
Ask the user for an integer
Use a for loop to generate the multiplication table
for the number (up to 10)
2
If the user entered 5, print the following
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
...
Solution
n =int(input())
foriinrange(1, 11):
print(n,’x’, i,’=’, n *i)
for loop Fibonacci
1 Ask the user for an integer, n (>=1)
Print the first nnumbers of the Fibonacci sequence
2
Solution
n =int(input())
a, b = 0, 1
print(a)
foriinrange(n-1):
print(b)
a, b = b, a+b
for loop over list 1
1 Ask the user for an integer, n
Use the range to create numbers from 1 to n
Print each value in this in a separate line
2
3
For example let us say user enters 3, print
1
2
3
Solution
n =int(input())
forxinrange(1, n+1):
print(x)
for loop over array
1 Ask the user for an integer, n
Use numpy’s linspace to create n points from 1
to 2
Print each value in this in a separate line
2
3
For example let us say user enters 3, print
1.0
1.5
2.0
Solution
fromnumpyimportlinspace
n =int(input())
data = linspace(1, 2, n)
forxindata:
print(x)
for loop over list 2
1 Ask the user for an list of fruits separated by a
comma
Print each fruit in a separate line
2
For example let us say user enters apple, pear,
print
apple
pear
Solution
fruits =input()
fruits = fruits.split(’,’)
forfruitinfruits:
print(fruit)
for loop over string
1
Ask the user for a string
Print each character of the string using a for loop
2
For example let us say user enters box, print
b
o
x
Solution
s =input()
forcharins:
print(char)
Nested for loops
1 Let us say the user supplies an integer, n (empty
prompt)
Print an n x n matrix where each entry is the sum
of the row + column
2
For example let us say user enters 3, print
0 1 2
1 2 3
2 3 4
Solution
n =int(input())
foriinrange(n):
forjinrange(n):
print(i+j, end=’’)
print()
Exercise: Fibonacci divisible by 4
1 Find the first number in the Fibonacci sequence
divisible by 4 but > 8 and less than 500.
Solution
a, b = 0, 1
whileb < 500:
ifb % 4 == 0andb > 8:
print(b)
break
a, b = b, a+b

More Related Content

Similar to Practice_Exercises_Control_Flow.pptx

error 2.pdf101316, 6(46 PM01_errorPage 1 of 5http.docx
error 2.pdf101316, 6(46 PM01_errorPage 1 of 5http.docxerror 2.pdf101316, 6(46 PM01_errorPage 1 of 5http.docx
error 2.pdf101316, 6(46 PM01_errorPage 1 of 5http.docx
SALU18
 
Intro to CS Lec03 (1).pptx
Intro to CS Lec03 (1).pptxIntro to CS Lec03 (1).pptx
Intro to CS Lec03 (1).pptx
FamiDan
 

Similar to Practice_Exercises_Control_Flow.pptx (20)

A formalization of complex event stream processing
A formalization of complex event stream processingA formalization of complex event stream processing
A formalization of complex event stream processing
 
Raspberry Pi - Lecture 5 Python for Raspberry Pi
Raspberry Pi - Lecture 5 Python for Raspberry PiRaspberry Pi - Lecture 5 Python for Raspberry Pi
Raspberry Pi - Lecture 5 Python for Raspberry Pi
 
5. Summing Series.pptx
5. Summing Series.pptx5. Summing Series.pptx
5. Summing Series.pptx
 
Python Programming
Python Programming Python Programming
Python Programming
 
Introduction to python programming
Introduction to python programmingIntroduction to python programming
Introduction to python programming
 
CPP Homework help
CPP Homework helpCPP Homework help
CPP Homework help
 
error 2.pdf101316, 6(46 PM01_errorPage 1 of 5http.docx
error 2.pdf101316, 6(46 PM01_errorPage 1 of 5http.docxerror 2.pdf101316, 6(46 PM01_errorPage 1 of 5http.docx
error 2.pdf101316, 6(46 PM01_errorPage 1 of 5http.docx
 
Python
PythonPython
Python
 
The Ring programming language version 1.3 book - Part 11 of 88
The Ring programming language version 1.3 book - Part 11 of 88The Ring programming language version 1.3 book - Part 11 of 88
The Ring programming language version 1.3 book - Part 11 of 88
 
Pseudocode By ZAK
Pseudocode By ZAKPseudocode By ZAK
Pseudocode By ZAK
 
III MCS python lab (1).pdf
III MCS python lab (1).pdfIII MCS python lab (1).pdf
III MCS python lab (1).pdf
 
Intro to CS Lec03 (1).pptx
Intro to CS Lec03 (1).pptxIntro to CS Lec03 (1).pptx
Intro to CS Lec03 (1).pptx
 
Programming in lua STRING AND ARRAY
Programming in lua STRING AND ARRAYProgramming in lua STRING AND ARRAY
Programming in lua STRING AND ARRAY
 
Introduction to python programming ( part-3 )
Introduction to python programming ( part-3 )Introduction to python programming ( part-3 )
Introduction to python programming ( part-3 )
 
Python.pptx
Python.pptxPython.pptx
Python.pptx
 
Working with tf.data (TF 2)
Working with tf.data (TF 2)Working with tf.data (TF 2)
Working with tf.data (TF 2)
 
Numerical method
Numerical methodNumerical method
Numerical method
 
Lec2_cont.pptx galgotias University questions
Lec2_cont.pptx galgotias University questionsLec2_cont.pptx galgotias University questions
Lec2_cont.pptx galgotias University questions
 
Data Handling
Data Handling Data Handling
Data Handling
 
Sol12
Sol12Sol12
Sol12
 

More from Rahul Borate

Unit 4_Introduction to Server Farms.pptx
Unit 4_Introduction to Server Farms.pptxUnit 4_Introduction to Server Farms.pptx
Unit 4_Introduction to Server Farms.pptx
Rahul Borate
 
Unit 3_Data Center Design in storage.pptx
Unit  3_Data Center Design in storage.pptxUnit  3_Data Center Design in storage.pptx
Unit 3_Data Center Design in storage.pptx
Rahul Borate
 
UNIT I Introduction to NoSQL.pptx
UNIT I Introduction to NoSQL.pptxUNIT I Introduction to NoSQL.pptx
UNIT I Introduction to NoSQL.pptx
Rahul Borate
 
Practice_Exercises_Files_and_Exceptions.pptx
Practice_Exercises_Files_and_Exceptions.pptxPractice_Exercises_Files_and_Exceptions.pptx
Practice_Exercises_Files_and_Exceptions.pptx
Rahul Borate
 

More from Rahul Borate (20)

PigHive presentation and hive impor.pptx
PigHive presentation and hive impor.pptxPigHive presentation and hive impor.pptx
PigHive presentation and hive impor.pptx
 
Unit 4_Introduction to Server Farms.pptx
Unit 4_Introduction to Server Farms.pptxUnit 4_Introduction to Server Farms.pptx
Unit 4_Introduction to Server Farms.pptx
 
Unit 3_Data Center Design in storage.pptx
Unit  3_Data Center Design in storage.pptxUnit  3_Data Center Design in storage.pptx
Unit 3_Data Center Design in storage.pptx
 
Fundamentals of storage Unit III Backup and Recovery.ppt
Fundamentals of storage Unit III Backup and Recovery.pptFundamentals of storage Unit III Backup and Recovery.ppt
Fundamentals of storage Unit III Backup and Recovery.ppt
 
UNIT I Introduction to NoSQL.pptx
UNIT I Introduction to NoSQL.pptxUNIT I Introduction to NoSQL.pptx
UNIT I Introduction to NoSQL.pptx
 
Confusion Matrix.pptx
Confusion Matrix.pptxConfusion Matrix.pptx
Confusion Matrix.pptx
 
Unit 4 SVM and AVR.ppt
Unit 4 SVM and AVR.pptUnit 4 SVM and AVR.ppt
Unit 4 SVM and AVR.ppt
 
Unit I Fundamentals of Cloud Computing.pptx
Unit I Fundamentals of Cloud Computing.pptxUnit I Fundamentals of Cloud Computing.pptx
Unit I Fundamentals of Cloud Computing.pptx
 
Unit II Cloud Delivery Models.pptx
Unit II Cloud Delivery Models.pptxUnit II Cloud Delivery Models.pptx
Unit II Cloud Delivery Models.pptx
 
QQ Plot.pptx
QQ Plot.pptxQQ Plot.pptx
QQ Plot.pptx
 
EDA.pptx
EDA.pptxEDA.pptx
EDA.pptx
 
Module III MachineLearningSparkML.pptx
Module III MachineLearningSparkML.pptxModule III MachineLearningSparkML.pptx
Module III MachineLearningSparkML.pptx
 
Unit II Real Time Data Processing tools.pptx
Unit II Real Time Data Processing tools.pptxUnit II Real Time Data Processing tools.pptx
Unit II Real Time Data Processing tools.pptx
 
2.2 Logit and Probit.pptx
2.2 Logit and Probit.pptx2.2 Logit and Probit.pptx
2.2 Logit and Probit.pptx
 
UNIT I Streaming Data & Architectures.pptx
UNIT I Streaming Data & Architectures.pptxUNIT I Streaming Data & Architectures.pptx
UNIT I Streaming Data & Architectures.pptx
 
UNIT I Introduction to NoSQL.pptx
UNIT I Introduction to NoSQL.pptxUNIT I Introduction to NoSQL.pptx
UNIT I Introduction to NoSQL.pptx
 
Practice_Exercises_Files_and_Exceptions.pptx
Practice_Exercises_Files_and_Exceptions.pptxPractice_Exercises_Files_and_Exceptions.pptx
Practice_Exercises_Files_and_Exceptions.pptx
 
blog creation.pdf
blog creation.pdfblog creation.pdf
blog creation.pdf
 
Chapter I.pptx
Chapter I.pptxChapter I.pptx
Chapter I.pptx
 
Polygon Mesh.ppt
Polygon Mesh.pptPolygon Mesh.ppt
Polygon Mesh.ppt
 

Recently uploaded

Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
negromaestrong
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
PECB
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 

Recently uploaded (20)

ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIFood Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Role Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptxRole Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptx
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 

Practice_Exercises_Control_Flow.pptx