Python 2
Python 2
2.2 Variable
A variable is a name that is a reference to an object.
You can use the assignment operator, “=” symbol, to assign values to variables.
The text to the left of “=” is the name of the variable.
The text to the right of “=” is the value that will be assigned to the variable.
Let’s try the following code and run. What did you see?
price = 5
After an assignment, if you type the name, the value stored in the name will be displayed.
stock = 15
stock
A name of a variable cannot start with a digit, and only letters, digits, or the underscore can
be used to form the name. Try the following codes:
Q1
Table 1
List of arithmetic operators
Operator Meaning
+ Add
- Subtract
* Multiply
/ Divide
% Modulus
** Exponent
== Equal to
!= Not equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
= x = 2 is the same as x = 2
+= x += 2 is the same as x = x + 2
-= x -= 2 is the same as x = x – 2
*= x *= 2 is the same as x = x * 2
/= x /= 2 is the same as x = x / 2
Function Meaning
abs(x) This function returns the absolute value of x.
eval(x) The function evaluates an argument x as a python expression
filter(x, y) This function is applied to filter out the items from an iterable x with the function y.
float(x) This function returns a number x into a float variable or converts a string into a float
variable
help(x) This function displays the built-in help system and provides the relevant information of
an object x.
input(x) The function displays the message x in string and takes user input as a string.
int(x) This function returns the integer part of the number x or converts a string x into an
integer.
isintance(x, y) This function tests and returns
True, when the stated object x is the stated type y.
False, if not
len(x) This function returns the number of characters of a string x.
list(x) This function is used to form a list object x.
max(x, y, z, …) This function finds the maximum value in the series of values x, y, z, and others.
min(x, y, z, …) This function finds the minimum value in the series of values x, y, z, and others.
pow(x, y) This function calculates x to the power of y.
print(x) The function is a simple way to produce an output x.
range(x,y,z) The function returns a sequence of numbers.
The first number starts from x
The sequence ends when the next number is higher than or equal to y.
z indicates increments of the sequence.
round(x, y) This function rounds a number x to a number with specified decimal places y.
slice(x, y, z) This function returns a slice object
The slice object starts from position x
The slicing ends at the position y
Z sets the step of slicing.
sorted(x, key=key, This function returns a sorted iterable x, such as a list or a dictionary.
reverse=reverse) In ascending order when reverse = False, which is the default
Key is the function for the sort comparison and is None by default.
str(x) The function converts an object x into a string.
sum(x, y) This function calculates the sum of the items in an iterable x and y.
2. Write a program that will get the perimeter of a square from a user and return the area of the
square.
4. Design a program to allow a user to input 3 numbers, and then return the largest number.
The terminal output would look like:
5. Design a small calculator that you can input a formula, and then return the result.
The terminal output would look like:
End of Module 2