baabtra.com little coder chapter - 6

25
L i t t l e C o d e r P r o g r a m by

Upload: baabtracom-no-1-supplier-of-quality-freshers

Post on 15-Jul-2015

334 views

Category:

Software


0 download

TRANSCRIPT

Little Coder Program

by

What you’ve learned?

How to compare conditions using if statements

What is block statements and importance of keeping equal spacing for

block statements

How to give else conditions

How to nest more than one If and else conditions

How to give multiple conditions inside the same if condition

LoopsChapter 6

Back to Anna !

• Do you remember this particular pink color block that you’ve used

for anna?

• We’ve used it whenever we wanted Anna to perform the same

instruction repeatedly. Similarly in python we use loops to repeat

some particular instructions

Loops

• That is, whenever you want a computer to repeat something over

and over again a loop instruction can be used. A loop is used to

make a computer do something more than one time.

Why Loops?

• Nothing is worse than having to do the same thing over and over

again. For example To print hello five times in Python, you could

do the following:>>> print("hello")

hello

>>> print("hello")

hello

>>> print("hello")

hello

>>> print("hello")

hello

>>> print("hello")

hello

“It’s not tideous !! Rather we can try with loop as below

which is much neater”

>>> for x in range(0, 5):

print('hello')

hello

hello

hello

hello

hello

“It’s not tideous !! Rather we can try with loop as below

which is much neater”

>>> for x in range(0, 5):

print('hello')

hello

hello

hello

hello

hello

The range function can be used to create a

list of numbers with in a range

For example

>>>print(list(range(10, 20)))

[10, 11, 12, 13, 14, 15, 16, 17, 18, 19]

Example

>>> for x in range(0, 5):

print('hello %s' % x)

Example

>>> for x in range(0, 5):

print('hello %s' % x)

Range(0,5) will return a list

of numbers from 0 to 5 as

below

[0,1,2,3,4,5]

Example

>>> for x in range(0, 5):

print('hello %s' % x)

[0,1,2,3,4,5]

xx is a variable which will

have value 0 when the loop

executes for the first time.

Example

>>> for x in range(0, 5):

print('hello %s' % x)

[0,1,2,3,4,5]

xOutput

hello 0

1st Iteration

Example

>>> for x in range(0, 5):

print('hello %s' % x)

[0,1,2,3,4,5]

xOutput

hello 0

hello 1

2nd Iteration

Example

>>> for x in range(0, 5):

print('hello %s' % x)

[0,1,2,3,4,5]

xOutput

hello 0

hello 1

hello 2

3rd Iteration

Example

>>> for x in range(0, 5):

print('hello %s' % x)

[0,1,2,3,4,5]

xOutput

hello 0

hello 1

hello 2

hello 3

4th Iteration

Example

>>> for x in range(0, 5):

print('hello %s' % x)

[0,1,2,3,4,5]

xOutput

hello 0

hello 1

hello 2

hello 3

hello 4

5th Iteration

Example

>>> for x in range(0, 5):

print('hello %s' % x)

[0,1,2,3,4,5]

xOutput

hello 0

hello 1

hello 2

hello 3

hello 4

hello 5

6th Iteration

Loop with lists

Fruits=[‘orange’,’apple’,’grape’,’pappaya’,’banana’]

for i in fruits:

print(i)

Here we have

created a list

variable named

fruits

Loop with lists

Fruits=[‘orange’,’apple’,’grape’,’pappaya’,’banana’]

for i in fruits:

print(i)

Now we are using

the variable fruits

instead of calling

range() function.

Loop with lists

Fruits=[‘orange’,’apple’,’grape’,’pappaya’,’banana’]

for i in fruits:

print(i)

Output

Orange

Apple

Grape

Pappaya

banana

While Loop

While loop

• A for loop isn’t the only kind of loop you can make in Python.

There’s also the while loop. A for loop is a loop of a specific

length, whereas a while loop is a loop that is used when you don’t

know ahead of time when it needs to stop looping.

>>> x = 45

>>> y = 80

>>> while x < 50 and y < 100:

x = x + 1

y = y + 1

print(x, y)

Exercise !

Exercise !

• Print all the numbers between 350 and 450 using for loop

• Print all the even numbers below 100 using while loop

• Print all the odd numbers between 100 and 200 using while loop

End of Chapter 6