Exercise sheet \(1\):

(i). Predict the output of following expressions.

(ii). Type them on Python shell and compare with your predictions.

Expresión

Predicción

Salida

Tipo

  1. 7-2

5

5

<class int>

  1. 4*1.5

6

6

<class float>

  1. 6/3

2.0

2.0

<class float>

  1. 6/-3

-2.0

-2.0

<class float>

  1. 6//-3

-2

-2

<class int>

  1. 6%3

2

2

<class int>

  1. 6.0%3

2.0

2.0

<class float>

  1. 6%3.0

2.0

2.0

<class float>

  1. -6%3

-2

-2

<class int>

  1. 6/-3.0

-2.0

-2.0

<class float>

  1. 2+4*3

14

14

<class int>

  1. (2+4)*3

18

18

<class float>

  1. “a”+”b”

“ab”

“ab”

<class str>

  1. “a”+”3”

“a3”

“a3”

<class str>

  1. 0 or 1

True

1

<class int>

  1. 0 and 1

False

0

<class int>

  1. 2 or not 2

True

2

<class int>

In an assignment statement, python evaluates the right-hand side first, before doing the actual setting of variables. Before typing following lines on shell, write your prediction in the dotted areas.

NumPy is the fundamental package for scientific computing with Python. It contains among other things:

  • A powerful N-dimensional array object.

  • Sophisticated (broadcasting) functions.

  • Tools for integrating C/C++ and Fortran code.

  • Useful linear algebra, Fourier transform, and random number capabilities.

SymPy is a Python library for symbolic mathematics. It aims to become a full-featured computer algebra system (CAS) while keeping the code as simple as possible in order to be comprehensible and easily extensible.

Matplotlib is a Python 2D plotting library which produces publication quality figures in a variety of hardcopy formats and interactive environments across platforms.

pandas is a fast, powerful, flexible and easy to use open source data analysis and manipulation tool, built on top of the Python programming language.

Exercise1_1.py
a)   5       <class 'int'>
b)   6.0     <class 'float'>
c)   2.0     <class 'float'>
d)   -2.0    <class 'float'>
e)   -2      <class 'int'>
f)   0       <class 'int'>
g)   0.0     <class 'float'>
h)   0.0     <class 'float'>
i)   0       <class 'int'>
j)   -2.0    <class 'float'>
k)   14      <class 'int'>
l)   18      <class 'int'>
m)   ab      <class 'str'>
n)   a3      <class 'str'>
o)   1       <class 'int'>
p)   0       <class 'int'>
q)   2       <class 'int'>
Exercise1_2_a.py
1
2
3
>>> x, y = 1, 2
>>> x = y
>>> y = x + y
Exercise1_2_b.py
>>> x, y = 1, 2
>>> x, y = y, x + y
>>> x
2
>>> y
3
Exercise1_3.py
>>> first = 'world'
>>> last = 'hello'
>>> print(last + ', ' + first)
hello, world
>>> print(last, ',', first)
hello , world