Unit 3
Unit 3
Unit 3
if Condition: do_this_if_true
do_this_if_true else:
else: do_this_if_false
do_this_if_false
LAB 7
1. Imagine that your computer program loves these plants. Whenever it
receives an input in the form of the word Rose, it involuntarily shouts to
the console the following string: "Rose is the best plant ever!"
2. Write a program that utilizes the concept of conditional execution, takes a
string as input, and:
a. prints the sentence "Yes - Rose is the best plant ever!" to the screen if
the inputted string is "Rose" (upper-case)
b. prints "No, I want a big Rose!" if the inputted string is "Rose"
(lower-case)
c. prints "Rose! Not [input]!" otherwise. Note: [input] is the string taken
as input.
3. Test your code using the data we've provided for you. And get yourself a
Rose, too!
LAB 8
● As you surely know, due to some astronomical reasons, years may be leap or
common. The former are 366 days long, while the latter are 365 days long.
● Since the introduction of the Gregorian calendar (in 1582), the following rule
is used to determine the kind of year:
● if the year number isn't divisible by four, it's a common year;
● otherwise, if the year number isn't divisible by 100, it's a leap year;
● otherwise, if the year number isn't divisible by 400, it's a common year;
● otherwise, it's a leap year.
● Look at the code in the editor – it only reads a year number, and needs to be
completed with the instructions implementing the test we've just described.
● The code should output one of two possible messages, which are Leap year
or Common year, depending on the value entered.
Solution for LAB 8
Loops in Python
1. There are two types of loops in Python: while and for:
● the body should be able Infinite loop Using a counter variable to exit a loop
to change the condition's
value, because if the
condition is True at the
beginning, the body might
run continuously to
infinity – notice that Using a counter variable to exit a loop
doing a thing usually
decreases the number of
things to do).
Continued
● The for loop executes a set of statements many times; it's used to iterate
over a sequence (e.g., a list, a dictionary, a tuple, or a set – you will learn
about them soon) or other iterable objects (e.g., strings). You can use the
for loop to iterate over a sequence of numbers using the built-in range
function.
● the for keyword opens the for loop;
Syntax of for loop
note – there's no condition after it; you
don't have to think about conditions, as
they're checked internally, without any
intervention;
● any variable after the for keyword is the
control variable of the loop; it counts
the loop's turns, and does it
automatically;
Continued
● the in keyword introduces a syntax element describing the range of possible
values being assigned to the control variable;
● the range() function (this is a very special function) is responsible for generating
all the desired values of the control variable; in our example, the function will
create (we can even say that it will feed the loop with) subsequent values from
the following set: 0, 1, 2 .. 97, 98, 99; note: in this case, the range() function starts
its job from 0 and finishes it one step (one integer number) before the value of its
argument;
● note the pass keyword inside the loop body – it does nothing at all; it's an empty
instruction – we put it here because the for loop's syntax demands at least one
instruction inside the body (by the way – if, elif, else and while express the same
thing) Write a program to print 5 table using
● Example of for loop both for and while loop
The range() function
● break – exits the loop immediately, and unconditionally ends the loop's
operation; the program begins to execute the nearest instruction after
the loop's body;
● continue – behaves as if the program has suddenly reached the end of
the body; the next turn is started and the condition expression is tested
immediately.
● Execute the beside code that
shows the functionality of
break
● Replace break with continue
and see what happens
The else branch
The loop's else branch is always executed once, regardless of whether the loop
has entered its body or not.
Example
LAB 9
● Listen to this story: a boy and his father, a
computer programmer, are playing with
wooden blocks. They are building a pyramid.
● Their pyramid is a bit weird, as it is actually a
pyramid-shaped wall – it's flat. The pyramid is
stacked according to one simple principle: each
lower layer contains one block more than the
layer above.
● The figure illustrates the rule used by the
builders:
● Your task is to write a program which reads the number of blocks the builders
have, and outputs the height of the pyramid that can be built using these blocks.
● Note: the height is measured by the number of fully completed layers – if the
builders don't have a sufficient number of blocks and cannot complete the next
layer, they finish their work immediately.
Solution of LAB 9
Sample Input:
1000
Expected output:
1. & does a bitwise and, e.g., x & y = 0, which is 0000 0000 in binary,
2. | does a bitwise or, e.g., x | y = 31, which is 0001 1111 in binary,
3. ˜ does a bitwise not, e.g., ˜ x = 240*, which is 1111 0000 in binary,
4. ^ does a bitwise xor, e.g., x ^ y = 31, which is 0001 1111 in binary,
5. >> does a bitwise right shift, e.g., y >> 1 = 8, which is 0000 1000 in binary,
6. << does a bitwise left shift, e.g., y << 3 = 128, which is 1000 0000 in
binary.
LAB 10
Write the code for leap year program defined in LAB 8 using logical operators
(single line condition)
Lists
● The list is a type of data in Python used to store multiple objects. It is an
ordered and mutable collection of comma-separated items between
square brackets
Eg:
Continued
● Lists can be iterated through using the for loop,
Eg:
7. You can delete slices using the del instruction Eg: del my_list[0:2]
8. You can test if some items exist in a list or not using the keywords in and
not in
Lists in Advance Applications
1. List comprehension allows you to create new lists from existing ones in a
concise and elegant way. The syntax of a list comprehension looks as
follows:
if conditional:
expression
Continued
2. You can use nested lists in Python to create matrices (i.e.,
two-dimensional lists). For example: