Using Python as a calculator¶
3**2
9
17 - 4
13
4/2
2.0
(5 + 4)*3
27
- The remainder can be calculated with the
%
operator
x = 2004
print(x % 100 == 0)
print(x % 4 == 0)
False True
- Powers With Python, use the ** operator to calculate powers.
# print(5^2) # ^ is not for power calculation
print(5**2)
print(5*2)
25 10
- Note Caret (^) invokes the
exclusive OR
(XOR) of the object: a logical operation that outputs true only when both inputs differ (one is true, the other is false).
- The equal sign (=) is used to assign a value to a variable. Afterwards, no result is displayed before the next interactive prompt.
a = 3
b = 5
c = a + b
c
8
Strings¶
- Python can also manipulate strings, which can be expressed in several ways. They can be enclosed in single quotes (
'...'
) or double quotes ("..."
) with the same result.
LastName = "Li"
FirstName = "Feng"
\
can be used to escape quotes:
print("Hello \\\\n World!")
Hello \\n World!
- If you don't want characters prefaced by
\
to be interpreted as special characters, you can use raw strings by adding anr
before the first quote:
print(r"Hello \n World!")
print("Hello \\n World!")
Hello \n World! Hello \n World!
- Strings can be concatenated (glued together) with the
+
operator, and repeated with*
:
"I " + 'L' + 'o'*5 + 've' + ' you'
'I Looooove you'
- The built-in function
len()
returns the length of a string:
print( ("Feng Li"))
print('love is "love" ')
Feng Li love is "love"
- Two or more string literals (i.e. the ones enclosed between quotes) next to each other are automatically concatenated. This feature is particularly useful when you want to break long strings:
"Feng" "Li"
'FengLi'
print("Hi, my name is Feng Li."
" And I am from Beijing.")
Hi, my name is Feng Li. And I am from Beijing.
- Strings can be indexed (subscripted), with the first character having index 0. There is no separate character type; a character is simply a string of size one:
Name = "Feng Li"
# 0123456
# ...-4-3-2-1
Name[1]
'e'
Name[-1]
'i'
Name[-2]
'L'
- In addition to indexing, slicing is also supported. While indexing is used to obtain individual characters, slicing allows you to obtain a substring:
# "Feng Li"
Name[0:4] # [a:b] - > [a, b)
'Feng'
Name[:4] # 0,1,2,3 4)
'Feng'
Lists¶
Python knows a number of compound data types, used to group together other values. The most versatile is the list, which can be written as a list of comma-separated values (items) between square brackets. Lists might contain items of different types, but usually the items all have the same type.
values = [1,5,7,9,12]
len(values)
5
values[0]
1
values[-2:]
[9, 12]
- Lists also supports operations like concatenation:
values + ["22","33"]
[1, 5, 7, 9, 12, '22', '33']
- Lists are a mutable type, i.e. it is possible to change their content:
values = [1,2,3,4,67,22]
values
[1, 2, 3, 4, 67, 22]
values[2] = 1000
values
[1, 2, 1000, 4, 67, 22]
You can also add new items at the end of the list, by using the append() method
values.append(9999)
values
[1, 2, 1000, 4, 67, 22, 9999]
- Assignment to slices is also possible, and this can even change the size of the list or clear it entirely:
[1, 2, 1000, 4, 67, 22, 9999] 0 1 2 3 4 5 6 [1, 2, 2, 3, 4, 67, 22, 9999]
values[2:4] = [2,3,4]
values
[1, 2, 2, 3, 4, 67, 22, 9999]
Built-in Functions¶
- The Python interpreter has a number of functions built into it that are always available. They are listed here in alphabetical order. Use e.g.
help(abs)
to see the function help.
abs() aiter() all() any() anext() ascii() bin() bool() breakpoint() bytearray() bytes() callable() chr() classmethod() compile() complex() delattr() dict() dir() divmod() enumerate() eval() exec() filter() float() format() frozenset() getattr() globals() hasattr() hash() help() hex() id() input() int() isinstance() issubclass() iter() len() list() locals() map() max() memoryview() min() next() object() oct() open() ord() pow() print() property() range() repr() reversed() round() set() setattr() slice() sorted() staticmethod() str() sum() super() tuple() type() vars() zip() __import__()
- See https://docs.python.org/3/library/functions.html for a complete description of those functions.
Import modules¶
To import a module (like math
) that is not in Python's default module, use
import math
Then you can use all the mathematical functions inside math
module as:
math.exp(0)
1.0
from math import exp
exp(0)
1.0
Defining Functions¶
We can create a function that writes the Fibonacci series to an arbitrary boundary.
The function should also include a proper help with the
docstring
The first line should always be a short, concise summary of the object's purpose.
If there are more lines in the documentation string, the second line should be blank, visually separating the summary from the rest of the description.
The following lines should be one or more paragraphs describing the object's calling conventions, its side effects, etc.
def fib(n):
"""Print a Fibonacci series up to n, 0, 1, 1, 2, 3, 5...
par n: integer
out : list
""" # the function help
a, b = 0, 1
while a < n:
print(a, end=' ')
# better than print(a), why?
a, b = b, a+b
help(fib)
Help on function fib in module __main__: fib(n) Print a Fibonacci series up to n, 0, 1, 1, 2, 3, 5... par n: integer out : list
fib(20000)
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711
Install third-party modules¶
- Use
pip
to install a Python module from PyPi (https://pypi.org) on your user directory.
! pip install numpy
- Update a Python module
! pip install pandas -U
- Remove a Python module
! pip uninstall numpy
! pip install pandas -U --break-system-packages
Defaulting to user installation because normal site-packages is not writeable WARNING: Skipping /usr/lib/python3.12/dist-packages/charset_normalizer-3.3.2.dist-info due to invalid metadata entry 'name' Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple Requirement already satisfied: pandas in /home/fli/.local/lib/python3.12/site-packages (2.3.2) Requirement already satisfied: numpy>=1.26.0 in /usr/lib/python3/dist-packages (from pandas) (1.26.4) Requirement already satisfied: python-dateutil>=2.8.2 in /usr/lib/python3/dist-packages (from pandas) (2.8.2) Requirement already satisfied: pytz>=2020.1 in /usr/lib/python3/dist-packages (from pandas) (2024.1) Requirement already satisfied: tzdata>=2022.7 in /home/fli/.local/lib/python3.12/site-packages (from pandas) (2025.2) WARNING: Skipping /usr/lib/python3.12/dist-packages/charset_normalizer-3.3.2.dist-info due to invalid metadata entry 'name'
Pyhon Zen¶
import this
The Zen of Python, by Tim Peters Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. Special cases aren't special enough to break the rules. Although practicality beats purity. Errors should never pass silently. Unless explicitly silenced. In the face of ambiguity, refuse the temptation to guess. There should be one-- and preferably only one --obvious way to do it. Although that way may not be obvious at first unless you're Dutch. Now is better than never. Although never is often better than *right* now. If the implementation is hard to explain, it's a bad idea. If the implementation is easy to explain, it may be a good idea. Namespaces are one honking great idea -- let's do more of those!