Python Chapter 1
Python Chapter 1
1. Python as a calculator
The Python interpreter acts as a simple calculator: you can type an expression at it and it will
write the value. Expression syntax is straightforward: the operators +, -, * and / work just like on
your regular calculator; parentheses can be used for grouping. For example:
>>> 1+3
4
>>> # This is a comment
>>> 2+2 # and a comment on the same line as code
4
>>> (60-5*6)/3
10
>>> 7/3 # Integer division returns the floor:
2
>>> 7/-3
-3
Remember that, by default, Python only has a limited set of keywords. For example, it only
knows how to do the basic mathematical operations (+,-,/,x). If you want a more scientific
calculator, you need to first import the math functions included in the module math:
From math import *
2. Python Variables
A variable is a name reference to a memory location. Variables provide an easy handle to keep
track of data stored in memory. Most often, we do not know the exact value of what is in a
particular memory location; rather we know the type of data that is stored there.
Python has three main types of variables:
-
Scalar variables hold the basic building blocks of data: numbers, and characters.
Array variables hold lists referenced by numbers (indices)
Dictionary variables hold lists references by labels.
The name of a variable can be practically any combination of characters and of arbitrary length.
Note that the type of a variable cannot usually not be guessed from its name: I strongly advise
7
you to choose a name for a variable that makes this type explicit. For example you can use
names like X, X_list, X_dic to define a scalar, a list, and a dictionary, respectively.
There are a few rules regarding variable names that you need to be aware of:
-
Creating a variable is as simple as making up a variable name and assigning a value to it.
Assigning a value to a variable is easy: all you have to do is write an equation, with the variable
name on the left, an = sign, and the value on the left. The = sign is called the assignment
operator:
>>>Width=4
>>>Height=3*12
>>>Area=Width*Height
>>>print Area
144
>>>x=y=z=0
>>>DNA=aattgcg
>>>Name_list=[John,David]
3. Special variable
Python has one special variable, _, that points to the place in memory that stores the more recent
result:
>>> 4+5
9
>>>print _
9
This special variable _ should be considered as read-only, i.e. I strongly advise against
assigning a value to it!!
4. Scalar variables
Python has two types of scalar values: numbers and strings. Both types ca be assigned to a scalar
variable.
4.1 Numbers
Numbers are specified in any of the common integer or floating point format:
>>>x = 1
>>>y = 5.14
>>>z = 3.25E-7
# Integer
# Floating point
# Scientific notation
Numbers can also be represented using binary or hexadecimal notations, but we will not need
that.
Table of the most common number operators in Python:
Operator
=
+
*
/
**
%
abs(x)
int(x)
float(x)
+=
-=
*=
/=
Meaning
Assign
Add
Subtract
Multiply
Divide
Exponentiation
Modulus
Absolute value of x
x converted to integer
x converted to float
Assign add
Assign subtract
Assign multiply
Assign divide
Python allows us to use all standard arithmetic operators on numbers, plus a few others. The
mathematical operations are performed in the standard order of precedence: power comes first,
then multiplication has a higher precedence than addition and subtraction: 2+3*4 is equal to 14,
and not 20. If we want the multiplication to be performed on 2+3, we need to include
parentheses: (2+3)*4. These are exactly the rules used by Python.
Some of the operators listed in the table above are unusual, and require more explanations:
The modulo operator:
i=52
j=3
k=i%j
In the example given above, the variable k holds the remainder of the division of 52 by 3, i.e. 1.
10
String operator
a+b
a*i
a[i:j:k]
a[::-1]
a.split(sep)
a.strip()
a.upper()
a.lower()
a.capitalize()
a.count(sub)
a.replace(sub1,sub2,n)
Meaning
Concatenates strings a and b
Repeats string a i times
Returns a string containing all characters of a
between position i and j, with step k; if k is
negative, starts from the right
Returns a string that is the reverse of a
Split string a into a list, using sep to decide
where to cut
Returns a string equal to a, but that has been
stripped of any white characters at the
beginning and end of a (space, tab, CR,)
Returns a string equal to a, but with all letters
uppercase
Returns a string equal to a, but with all letters
lowercase
Returns a string equal to a, but with the first
word capitalized
Counts the number of instances of the
substring sub in the string a
Returns a string equal to a, but with n instances
of substring sub1 replaced with substring sub2;
if n is not given, all instances are returned
Concatenating strings:
The + operator, when placed between two strings, creates a single string that concatenates the
two original strings. In the following example:
#
>>>A==ATTGC
>>>B=GGCCT
>>>C=A+B
The variable C contains the string ATTGCGGCCT. Note that the concatenation operator can
be attached to an assignment:
C+=G;
Adds a G at the end of the string contained in C.
Repeating a string
The operator * repeats a string a given number of times:
11
>>> text=No!
>>>newtext=text*5
>>> print newtext
No! No! No! No! No!
12
Examples:
>>> S = This is a string
>>> b = S[1:3]
>>> print b
hi
>>> S[5:12:3]
iat
>>> S[1:5:-1]
>>> S[5:1:-1]
i si
>>> S[10::]
string
>>> S[::-1]
gnirts a si sihT
The other string manipulations described below apply a function on the string. The syntax is:
string.function(argument)
where string is the string considered, function is the function applied, and argument are
parameters for the function, if any.
Breaking a string into a list
A string can be broken down into a list using the function split. The syntax is:
A.split(sep)
where A is the string, and sep the separator. If sep is not provided, Python uses the white space.
Examples:
>>>text=This is a test case; it has two parts
>>>text.split()
[This,is,a,test,case;,it,has,two,parts]
>>> text.split(;)
[This is a test case, it has two parts]
>>> text.split(a)
[This is , test c,se; it h,s two p,rts]
13
Striping a string
A string may have leading or lagging white characters, such as blanks, tabs, or carriage return. It
is a good idea to remove those, using the function strip().
Changing case
-
Replace
Replace is a function that substitutes a string for another:
String.replace(sub1,sub2,n)
String is the string on which replace is applied; n instances of sub1 are replaced with sub2; if
n is not provided, all instances of sub1 are replaced.
14
Question is the string printed on the screen to let the user know what he/she needs
to input
answer is a string that stores the answer of the user.
Note that the result of raw_input is always a string. If you expect an integer or a float from the
user, you need to change the type:
age = int(raw_input(What is your age :))
age is now an integer that contains the age entered by the user.
15
Exercises:
1. Without the aid of a computer, work out the order in which each of the following
expressions would be computed and their value.
i. 2 + 6/4-3*5+1
ii. 17 + -3**3/2
iii. 26+3**4*2
iv. 2*2**2+2
Verify your answer using Python.
2. Without the aid of a computer, work out these successive expressions and give the
values of a, b, c and d upon completion. Then check your answer using a Python
script:
a=4
b=9
c=5
d= a*2+b*3
$c+=-$d/3
b%=a
a=b-1;
3. Write a Python program that:
i. Reads a sentence from standard input
ii. Writes this sentence on standard output all in lower case
iii. Writes this sentence on standard output with all vowels in upper case and
all consonants in lower case
iv. Writes the sentence in reverse order
4. Write a Python program that:
i. Reads a sentence from standard input
ii. Counts the number of words and the number of characters, not included
space
iii. Counts the number of vowels.
5. Write a Python program that reads from standard input the amount of a restaurant
bill and outputs two options for tipping, one based on 15% of the bill, the other
based on 20% of the bill.
6. Write a Python program that:
i. Reads a sentence
ii. Remove all vowels
iii. Replaces all v and b in the original sentence with b and v, respectively (i.e.
for example string cvvbt becomes cbbvt
iv. Count number of letters in the modified sentence
v. Writes the resulting sentence and number of letters on standard output
16