G6 Computers Ch6 Python-2
G6 Computers Ch6 Python-2
Unit 6:Python -2
Overview:
unit continues on the basic
to build
This Python
IF-ELIF-ELSE, concepts
about FOR and WHILE introduced in
Grade 5.
learn loops. Students will
have been provided using Programming solutions to
problems conditional and loop constructs. simple
unit, students are numerical
this exp0sed
Through to various
constructs of
program solutions to problems.
a
write programminglanguageand
.
Essential Questions:
What makes a program efficient?
How can programs simulatereal
world problems and solutions2
Enduring understanding:
Students will understand that:
An program uses less memory,
efficient
considers all possible user input and takes care of
all foreseeableerrors that could take place while
the program runs.
• A real world problem can be simulated by inputting necessary data and quantifying the
problem.Object oriented programming languages have many features that allow us to
model real world situations in programming terms.
Content:
Essential
Vocabulary:
Punctuators, While,
Variables, Operators,
Coding, IDLE, Syntax, Keywords, For, ldentifiers,
Boolean, String, User Input,
Separators, Arithmetic, Break, Float, Integer,
Relational, Data types,
Conditional, Sequential, Function, Builtin,Iterative, List, Range
59
EKYA
SCHOOLS
Unit 6 -PYTHON
A. Quick Recap
We Python by double clicking on the Python shortcut icon on the
start
desktop or on the taskha.
You can use Cortanato search for IDLE and open it.
also
Python 68 Shell
Fie Et Shelbug Opthens Windew Help
Print function in Python 3 version performs the same task as the Python print statement in
2.7. It
helps us display things. If you want to print some text like the example given below,
you put the text
within quotes. Python allows both single as well as double quotes for inputting text.
HeltoWorid
>>>in(5."r tgy ater)
5is my lucky number
60
EXYA
SCHOOLS
CMR
A.2 ScriptMode in Python:
A Script in Python is a
setof code that is saved
A script is also known
as a file. The extension for a
as a module or a program. To Python script file is .py.
1. Click on File in IDLE. create a new
script/program/module in Python:
2. Click on New File.
Python3.68She
File Edit Shell Debug Options Window
New File
Open... Ctrl+O
: Help
3c6b436a57, Dec 24 20
Save Ctri+S
Ciose
Exit
The New File that opens is a text editor and looks like this:
Menu Options
61
EKYA
SCHOOLS
CMR
gebeyod
Untitled
2. You will see a dialog box open that asks you to save your program first. Give
a suitable na
for the program and save it. In Windows, the default location is the Python folder
OK Cancel
UntitedR
Save As
Favourites Nne
ate modified
Type
Desktog DLS
Downlcads 2-01-210
Recent places
inciude
Lit
Homegroup bs -0-20SNOS
Scripts
This PC
tct
Desktop Tools
2-01-2019 90S
Documents 02-01-2019 09:05 Fite foier
Downloads
Music
File nane: gr
first
Save as type: Python files
Hide Folders
Save Cancel
62
EKYA
SCHOOLS
CMR (go tseyond
3. IDLE pops up
with the output:
A - B
Subtraction operation
A <> B Checks ifA is not equal to B. There is no difference between <> and !=.
A != B
63
EKYA
SCHOOLS
input) accepts any value given by the user and assigns it to the
variable on the left hand side. The
text that appears inside the input() is the prompt given to the user. If
yoU want to ask the user to
input his name, then the prompt can be
'Enter your name.
If the user has to input an integer, we use int) which encloses input(). If the user has to input
decimal fractions, we use float().
B. IF Statements in Python
Ifstatement checks a
if condition is true. We have already seen how
conditions can be
checked in the Algorithmsand
Flowchartsunit. When a condition is true, one branch of the
program gets executed.Otherwise,
anotherbranch of the program gets
statements form a block of statements.(A executed. In Python, IT
block is a set of statements
which are executed as
a unit.)
EKYA
SCHOOLS
CMR
B.1 Simple IF Statement
Syntax for asimple IF statement:
In this case, a condition is tested. If it is true, then a
if <condition to be tested>: code block is executed.
<statementsto be executed>
Usingsimple IF statement:
The program given below checks
the integer entered
if
is positive. We will get an output only for
positive integers.
Enter an integer 9
9is positive
Enter an integar-5
But wouldn't it be more informative if we had an output when the entered number is not positive? We need
an IF-ELSE block in order to achievethis.
B.2 IF-ELSEStatement
Syntaxfor IF-ELSEStatement:
if <condition to be tested>:
<statementsto be executed if condition is true>
else:
65
EKYA
SCHOOLS
CMR
Using IF-ELSEStatement:
Let us add the ELSE clause in our program to find an integer
if is positive. If it is not positive ue
display an output that says the number is not
positive.
Enter an integer 6
S is positive
Enter an integer-6
-6 is negative
Marks Grade
Marks < 35 F
<statements>
6
EKYA
SCHOOLS
CMR
elif <condition2>:
<statements>
elif <condition3>:
<statements>
else:
<statements>
mprint(Name",
> 75 a9
print(Grade:
name)
<
A)#Assign
100: #arc cCperator
gradeA
hecks fo both inmts sirssaneosiy
elif m >=35:
print(Grade: C)#Assign grade C
eise:
print(Grade: FAssign gradeF
Output:
Name: Ria
Grade: A
zz
Enter your name Gaurav
REST
Name: Gaurav
Grade: C
67
EKYA
SCHO0LS
CMR
Points to
and
ponder :
operator ensures that marks are in the range 75 to 100. It is a logical
operator. There
are three Logical operators in Python: not, and, or. When we want to check if
conditions are simultaneously true, we use and. When we want to check if any multiple
one of the
Conditions is true out of many, we use or.
o The upper limitof marks is not checked in the elif statements.Why do you think we e.
not check it?
o What happens if a user inputs a negative number? How can we modify the program such
that theuser gets amessage Invalid input' if negative numbers are input.
n int(input(Enter
print(n, isneutral)
Output:
Enter an integer 4
4 is positive
0 is neutrai
-9 isnegative
68
EKYA
SCHO0LS
|
Python 3 returns an iterable
object.What this means is that it
range(0, 5)
holds a set of numbers that we >>> list(range(5)
willtypically use in loops. How do
we know what these numbers 0,1,2,3. 4.
are? |>>>list(range(2,20,2)
As seen in the output, we cannot
>>> list(range(10,3))
A list in Python is a series of
69
EKYA
SCHOO.S
Python assumes to be 1. it
>>>list(range(10,3,-1)
If two arguments are given,
value
will not be included in the
list.
round(x [)
n] This function returns a float value
by rounding off a value upto n
>>> rond(5.67,1)
digits.
>>> round(5.6777, 2)
C.Loops in Python
We use the range function to create an iterable based on the start, >>> for in range(1,11):
i
print(i)
stopand step values. The variable istands for each of the numbers
present in the iterable object.
Notice that the stop value is 11 and not 10. range(1,11) returns the
the above
Example 2:What would be the output for
>>> foriin range(30, 20, -3):
exarnple?
print(i)
Let us create the list thatwould be created with the range
function: (30,27,24,21].
So, the output should display each of
these elements.
Output:
27
24
21
71
EKYA
SCHOOS
CMR
Output:
Priya
3x1=3
3 x 2 =6
3 x3=9
Enter a nurber l6
RSAR /0se
So,the print statement looks like this: 16 1 16
n xi=n*i 16 2 32
16 3 48
Output: 16
12
EKYA
SCHOOS
CMR
Output:
We can use the replication operator with strings only when there is one
Python returns an error.
>>>S='racecar
>> for i in s:
print (i, end )
a sequence that has elements, just like a list. The variable
Here s is a string, not a list. But, s is still
takes on the values from the string -r, 'a, ',e'
etc.
i
Output:
Eace
73
EKYA
SCHOOLS
CMR
In a while loop, we can test a condition. As long as the condition is true, the block of statermars.
keeps getting executed. When the condition is no longer satisfied, control passes to the immediate
statement after the while block.
Example 1: Accept a number from the user and print 'Hello that many times using a while In
(Note:This program is easier using FOR. This example is given to demonstrate how the While loon
works.)
Output:
Hello
Hello
Hello
Hello
Hello
#1
print (Hello ')
#Thisis the same asn=n-1
EKYA
SCHOOLS
CMR
Output:
The word Hello keeps printing on IDLE and the program
execution willnot
end unless we stop it by closing IDLE. He1lo
Hel1o
Hello
If an updatestatement is not given in a while block, we will have an infinite loop. An infinite loop
numbers 1 to n as output.
nninputtEntea unba greater han or equal to 1 )
Output: print (0)
i+*1Move to ne next number
75
EKYA
SCHOOLS
CMR go
tbeyons
Example 3: Acceptnumber n1 and number n2. Print thefirst 'n2' multipless of 'n1'. If n1 = 5and
n2 =
3, then the program needs to print the first 3 multiples of 5.
#print
n1
n2
|=1
while
=
n2 multiples
int(input(Enter
int(input(Enter
< n2:
of a number, n1
a number )
number of multiples )
using while loop
i
print (Multiple', I, is, n1"i)
Output:
Enter a number 6
Enter nunber of multiples
Multiple 1 is 6
Multiple 2 is 12
Multiple 3 is 18
Multiple 4 is 24
Multiple 5 is 30
Multiple 6 is 36
4ultiple 7 is 42
76