Professional Documents
Culture Documents
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 — 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
X NOT X
True False
False True
While Loop
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
Python Turtle
Python Turtle is a special package in Python that is used to draw shapes.
Turtle Functions
fillcolor Sets the colour (RGB) that the turtle will use to fill a shape
3
Dodge the Dynamite
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
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.
4
Dodge the Dynamite
Calling a Function
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.
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
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.
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.
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).
7
Dodge the Dynamite
Practice Questions
Answers
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.