You are on page 1of 3

P ROGRAMMING I

A SSIGNMENT 1, D UE D ATE F RIDAY 11, 2015 - 11:59 PM

Expressions & Variables

September 6, 2015

1. Operator Precedence and Associativity:


Operator precedence in Python is given by the following table:

https://docs.python.org/3/reference/expressions.html#operator-precedence
You can use parenthesis in an expression so that the precedence rules become explicit.
For example given:

5 * 4 * 2 + 4 / 4 ** 2 ** 4 + 1
You can successively apply the precedence rules to get the following sequence:

5 * 4 * 2 + 4 / 4 ** (2 ** 4) + 1
5 * 4 * 2 + 4 / (4 ** (2 ** 4)) + 1
(5 * 4) * 2 + 4 / (4 ** (2 ** 4)) + 1
((5 * 4) * 2) + 4 / (4 ** (2 ** 4)) + 1
((5 * 4) * 2) + (4 / (4 ** (2 ** 4))) + 1
(((5 * 4) * 2) + (4 / (4 ** (2 ** 4)))) + 1
Do the same for the following expressions:
a) 5 * 6 / 7 * 8 / 9
b) 5 * 6 / 7 ** (8 / 2) * 7 + 4
c) True or False and True or True == False or False
d) 456 % 10 + 456 // 10 % 10 + 456 // 100 % 10

2. Naming Abstraction
Study the following rules and conventions for identifier names in Python.
Rules for writing identifiers in Python:
a) Identifiers can be a combination of letters in lowercase (a to z) or uppercase (A
to Z) or digits (0 to 9) or an underscore (_). Names like myClass, var_1 and
print_this_to_screen, all are valid example.
b) An identifier cannot start with a digit. 1variable is invalid, but variable1 is
perfectly fine.
c) Keywords cannot be used as identifiers.
d) We cannot use special symbols like !, @, #, $, % etc. in our identifier.
Conventions for writing longer names:

Camel Case

CamelCase is a naming convention in which a name is formed of multiple words that are joined together as a single word with the first letter of each
of the multiple words capitalized so that each word that makes up the name can
easily be read. The name derives from the hump or humps that seem to appear in
any CamelCase name. You can choose whether to capitalize the first letter or not.
Examples: listHead, incrementByOne, AnInteger

Snake Case

Snake case (or snake_case) is the practice of writing compound words


or phrases in which the elements are separated with one underscore character
(_) and no spaces, with each elements initial letter usually lowercased within
the compound and the first letter either upper or lower case-as in foo_bar and

Hello_world

Naming conventions in Python are described here: https://www.python.org/dev/

peps/pep-0008/#naming-conventions
3. Translating formulas into Python

Suppose we are given that the height of a cylinder is 20 cm and its radius is 5 cm. To
calculate and store the volume of the cylinder in Python we can use the following statement:

pi = 3.14
height = 20
radius = 5
volume_of_cylinder = pi * (radius ** 2) * height
Do the same for the following problems. Remember to use either camel case or snake
case for compound names.
a) In order to make burger a chef needs the following ingredients:
one piece of chicken meat
3 lettuce leaves

6 tomato slices
Write down a formula (use identifier as in the example above) to figure out how
many burgers can be made. Hint: use Pythons built-in function min
b) The surface area of a right-circular cone is given by:
r (r +

h2 + r 2)

Calculate the surface area by writing down the formula (using variables with appropriate names) in Python. Hint: use the sqrt and pi from Pythons math library
4. Translate the following equations into Python (do not use variables)
a)

35
2+3

p
b) 7 + 9 2
c) (4 7)3
p
4
d) 19 + 100
e) 6 mod 4

You might also like