3
PROGRAMMING I ASSIGNMENT 1, DUE DATE FRIDAY 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: You can use parenthesis in an expression so that the precedence rules become explicit. For example given: You can successively apply the precedence rules to get the following sequence: Do the same for the following expressions: a) b) c) d) 1

Assigfjjfjfjf

Embed Size (px)

DESCRIPTION

ndnjjjrjrj

Citation preview

Page 1: Assigfjjfjfjf

PROGRAMMING IASSIGNMENT 1, DUE DATE FRIDAY 11, 2015 - 11:59PM

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

1

Page 2: Assigfjjfjfjf

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 (Ato Z) or digits (0 to 9) or an underscore (_). Names like myClass, var_1 andprint_this_to_screen, all are valid example.

b) An identifier cannot start with a digit. 1variable is invalid, but variable1 isperfectly 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 mul-tiple words that are joined together as a single word with the first letter of eachof the multiple words capitalized so that each word that makes up the name caneasily be read. The name derives from the hump or humps that seem to appear inany 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 wordsor phrases in which the elements are separated with one underscore character(_) and no spaces, with each element’s initial letter usually lowercased withinthe compound and the first letter either upper or lower case-as in foo_bar andHello_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. Tocalculate and store the volume of the cylinder in Python we can use the following state-ment:

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 snakecase for compound names.

a) In order to make burger a chef needs the following ingredients:

• one piece of chicken meat

• 3 lettuce leaves

2

Page 3: Assigfjjfjfjf

• 6 tomato slices

Write down a formula (use identifier as in the example above) to figure out howmany burgers can be made. Hint: use Python’s 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 ap-propriate names) in Python. Hint: use the sqrt and pi from Python’s math library

4. Translate the following equations into Python (do not use variables)

a) 3×52+3

b)p

7+9×2

c) (4−7)3

d) 4p−19+100

e) 6 mod 4

3