Sie sind auf Seite 1von 4

Name: _____________________________ Date:_______

Task 1: Exercises: Use Python 2.7 to complete the following Exercises.


Operators
Python commands are called statements. One kind of statement is an expression statement, or expression for short. You are familiar with mathematical expressions like 3 + 4 and 2 - 3 / 5; each expression is built out of values like 2 and 3 /5 and operators like + and -, which combine their operands in different ways. Like any programming language, Python can evaluate basic mathematical expressions. 1. For each of the following expressions, what value will the expression give? Verify your answers by typing the expressions into Python. a. 12-5 b. 10*2.5 c. 12/2 d. 12/-2 e. 12%2 f. 12%-2 g. -12%2 h. 12/-2.0 i. 4+5*5 j. (4+5)*5

Name: _____________________________ Date:_______ TASK 2: Functions


The general form of a function denition is as follows: def function_name(parameters): block Ex. To converted from degrees Fahrenheit to Celsius, the python script looks like: >>> def to_celsius(t): ... return (t - 32.0) * 5.0 / 9.0 2. Use Python to create the following equations: a. vx= vi*cos(theta) b. impulse = mu*mass*gravity*time c. work = force *distance

Name: _____________________________ Date:_______

Strings In Python, we indicate that a value is a string by putting either single or double quotes around it. Enter the following strings at the shell: 3. >>> 'Nikolai' 4. >>> Tesla Joining strings
Its almost always clearer to join strings with +. When + has two string operands, then it is referred to as the concatenation operator:

>>> 'Nikki' + ' Giovanni'


The shortest string is the empty string, containing no characters at all. It is the textual equivalent of 0adding it to another string has no effect.

5. >>> ''

Python will not combine strings and numbers using + . However this doesnt mean that other operators cant combine strings and integers. In particular, we can repeat a string using the * operator, like this. Type this statement at a python prompt. What does this yield? 6. >>> 'VANGAURD' * 5 Suppose you want to put a single quote inside a string. If you write it directly, Python will complain. Type this statement at a python prompt. If there is an error, fix it and write the corrected statement using python. 7. >>> 'We've got to fix this'

Name: _____________________________ Date:_______

Boolean Operators
There are only three basic Boolean operators: and, or, and not. not has the highest precedence, followed by and, followed by or. not is a unary operator; in other words, it is applied to just one value, like the negation in the expression -(3 + 2). An expression involving not produces True if the original value is False, and it produces False if the original value is True:
For each of the following expressions, what value will the expression give? Verify your answers by typing the expressions into Python. a) True and not False b) True or False and True c) not True or not False d) True and not 0 e) 95 < 52.3 f) 1+45>52.3 g) 10 != 4.0

To see how these functions are used, lets go through all the pixels in Tenickas picture and make it look like it was taken at sunset. To do this, were going to remove some of the blue and some of the green from each pixel, making the picture darker and redder.

Das könnte Ihnen auch gefallen