You are on page 1of 8

Dodge the Dynamite

Decision-Making in programming

A computer can only make simple yes or no decisions. Hence, when communicating with a computer, we
need to ask simple questions that can be answered in yes or no.`

Boolean Logic
A computer can think of only one of two options at a time:
Yes or No
True or False
1 or 0
This type of thinking is called Boolean Logic.

Logical Operators
It is possible to combine Boolean expressions using the following logical operators:
AND
OR
NOT

Boolean Logic — AND


Let us suppose ‘X’ and ‘Y’ are the inputs. When ‘X’ and ‘Y’ are combined using the operator ‘AND’, the
expression ‘X AND Y’ is the output.This expression will return ‘True’ only when both ‘X’ and ‘Y’ are ‘True’.
X Y X AND Y
True True True
True False False
False True False
False False False

Boolean Logic — OR

When ‘X’ and ‘Y’ are combined using the operator ‘OR’, the expression ‘X OR Y’ is the output.This expression
will return ‘False’ only when both ‘X’ and ‘Y’ are ‘False’.
X Y X OR Y
True True True
True False True
False True True
False False False

1
Dodge the Dynamite

Boolean Logic — NOT


The NOT operator works on a single Boolean value. If ‘X’ is ‘True’, then ‘NOT X’ will return False as the
output.

X NOT X
True False
False True

Boolean Logic Table


The following is called a Boolean Logic Table. It shows the output for the different combinations of
Boolean operators.
X Y X AND Y X OR Y NOT X
True True True True False
True False False True
False True False True True
False False False False

While Loop

A while loop runs until its given condition is True.


The syntax of a while loop is as follows:
while condition:
statement(s)_to_execute

Infinite Loop

If the condition stated in the while loop never switches to False, the loop will run forever. This is called an
infinite loop.
x = 2 2
while x > 1: 3
print(x) 4
x = x + 1 5
...

In the example given above, the value of x keeps on increasing. Hence, the condition stated in the while
loop will never become False and the loop will run forever, printing 2 onwards.

2
Dodge the Dynamite

For Loop
It repeats a set of steps a specified number of times.
The syntax of a for loop is as follows:
for x in range/list:
statement(s)_to execute

The range() Function


The range() function in Python generates a sequence of numbers.
Writing x = range(5) is the same as writing x = [0,1,2,3,4].

Python Turtle
Python Turtle is a special package in Python that is used to draw shapes.

Terms Used in Python Turtle


Listed below are some of the most important terms used in Python Turtle.
Python Turtle The name of the package
Canvas The white drawing screen
Turtle The on-screen pen used for drawing
Turtle functions Commands to move the turtle

Turtle Functions

forward Moves the turtle forward

backward Moves the turtle backwards

right Turns the turtle right

left Turns the turtle left

up Picks up the turtle pen

down Puts the turtle pen down

color Sets the colour of the turtle pen

fillcolor Sets the colour (RGB) that the turtle will use to fill a shape

3
Dodge the Dynamite

goto Moves the turtle to position (x, y)

Tells the turtle that the shape is being created and it needs to be filled with the chosen
begin_fill
fill colour

Tells the turtle that the shape has been created and it can now be filled with the
end_fill
chosen fill colour

dot Draws a dot at the current position

stamp Draws an impression of the turtle shape at the current position

shape Should be ‘arrow’, ‘classic’, ‘turtle’, ‘circle’, or ‘square’

bgcolor Changes the background colour of the screen

Functions and Libraries

Functions: With the help of a function, a set of instructions can be called by a single name.
Functions and libraries allow for the code to be reused, which helps save time and keeps the code short.

Every function has a purpose. The code written inside a function is used to perform a specific task.

Function name
Function
body
Keyword Parameter

In the example given above, we have defined a function named greeting. This function has a parameter
named guestName.

The purpose of this function is to greet the guest with a hello.

4
Dodge the Dynamite

Calling a Function

A function can be called by using its name.


Once a function is defined, it needs to be called using an appropriate parameter.
Using the function defined on the previous page, let us see how a function can be called.

Functions with Multiple Parameters


A function can receive more than one input parameter.

Ensure that while calling a function you provide it with a value for every parameter. Calling the function
mentioned above with just one guest name will return an error.

Function and Return


Functions are often used to return a value. The keyword return tells Python what output is to be returned.

Libraries
Libraries are collections of functions defined by other developers. Using the keyword import, we can
access the functions defined in that library.

5
Dodge the Dynamite

What is a data structure?


A data structure is a collection of data organised and managed in a particular way.
In this unit, we are going to learn about the following data structures used in Python:
List
Tuple
Dictionary

Lists
A list is a collection of data written inside square brackets, separated by commas. For example:

Here, the name of the list is fruits. It contains 4 strings inside it.
A list can contain strings of different datatypes as well.

Indexing and Accessing


Each item stored inside a list has an index or a position.

0 1 2 3
The indexing of a data structure always starts from 0.
So, “banana” is at position 0, “apple” is at position 1, “orange” is at position 2, and “mango” is at position 3.

We can access the items of a list using their index/position.

Tuples

A tuple is a collection of data written inside round brackets, separated by commas. Just like a list, a tuple
can also contain strings of different datatypes. For example:

6
Dodge the Dynamite

We can access the items of a tuple in the same way as we access the items of a list.

Dictionaries
A dictionary is a collection of data written inside curly brackets, separated by commas.
A dictionary stores values as key:value pairs, as shown in the following example.

key : value

What is a queue?
A queue is a linear list of elements in which the deletion of an element can take place only at one end
(called the front) and insertion can take place at the other end (called the rear).

Queue — First in First out


A queue follows the concept of FIFO (First in First out), which means that the first element that is inserted
in the queue will be the first element to be deleted or removed from the queue.

7
Dodge the Dynamite

Practice Questions

1. State whether the following statements are true or false.


(a) A while loop runs until its given condition is False.
(b) A dictionary stores values as key:value pairs.
(c) A computer can think of only one of two options at a time — True or False.
(d) The NOT operator works on a single Boolean value.

2. Fill in the blanks.


(a The keyword _____________________ tells Python what output is to be returned.
(b) Each item stored inside a list has an _____________________ or a position.
(c) 
A _____________________ is a collection of data written inside square brackets, separated by
commas.

3. Answer the following questions in brief.

(a) What is a queue?

(b) What is a tuple?

(c) What is the use of the range() function?

Answers

1. (a) False (b) True (c) True (d) True

2. (a) return (b) index (c) list

3. (a) A queue is a linear list of elements in which the deletion of an element can take place only at one
end (called the front) and insertion can take place at the other end (called the rear).

(b) A tuple is a collection of data written inside round brackets, separatedby commas. It can contain
strings of different datatypes.

(c) The range() function in Python generates a sequence of numbers.


Writing x = range(5) is the same as writing x = [0,1,2,3,4].

You might also like