Types & Programs¶

Two somewhat unrelated topics, but important nonetheless are Types and Programs.

Types¶

We'll learn a lot of types throughout this quarter, but at this point there are four that we know about:

  • Integer (whole numbers): int
  • Floating point (approximate): float
  • Boolean (yes/no): bool
  • String (arbitrary text): str

You can inspect the type of a given value using the type function:

In [1]:
type(4.0)
Out[1]:
float
In [2]:
type("Hello")
Out[2]:
str
In [3]:
type(42)
Out[3]:
int
In [4]:
type(3 < 5)
Out[4]:
bool
In [8]:
x = 3
type(x)
type(3)
Out[8]:
int
In [9]:
x = 4.0
type(x)
Out[9]:
float
In [11]:
x = "Hello"
type(x)
Out[11]:
str

Using different types in an expression usually doesn't work. But some data types "kind of look the same from a distance." For example, floats and ints are both numeric, and so can be used together. However, change happens!

In [12]:
3 + 4.0
Out[12]:
7.0
In [13]:
3 < 4.0
Out[13]:
True
In [14]:
3 / 4.0
Out[14]:
0.75
In [19]:
0.1 + 0.1 + 0.1
Out[19]:
0.30000000000000004

Strings are especially hard to combine with other data types as they're quite different. (Consider: what would it mean to do "Hello" - 3?).

In [20]:
"Hello" - 3
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[20], line 1
----> 1 "Hello" - 3

TypeError: unsupported operand type(s) for -: 'str' and 'int'
In [21]:
3 + "a"
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[21], line 1
----> 1 3 + "a"

TypeError: unsupported operand type(s) for +: 'int' and 'str'
In [22]:
"aaa" - 3
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[22], line 1
----> 1 "aaa" - 3

TypeError: unsupported operand type(s) for -: 'str' and 'int'
In [23]:
"aaa" / 3
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[23], line 1
----> 1 "aaa" / 3

TypeError: unsupported operand type(s) for /: 'str' and 'int'
In [24]:
"a" * 3
Out[24]:
'aaa'

Some combinations and operations give potentially unexpected results:

In [25]:
3 / 4
Out[25]:
0.75
In [26]:
3 // 4
Out[26]:
0
In [27]:
3 + True
Out[27]:
4
In [29]:
3 + False
Out[29]:
3
In [28]:
False + True
Out[28]:
1
In [31]:
3 / 0
---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)
Cell In[31], line 1
----> 1 3 / 0

ZeroDivisionError: division by zero
In [33]:
3 / (2 < 5)
Out[33]:
3.0
In [35]:
3 // 1
Out[35]:
3

Programs¶

All of the homework assignments in CSE 160 deal with "programs." A program is like a recipe, allowing you to save the instructions and when you want to use the program, runs the instructions from top to bottom.

(NB: Notebooks deal with "cells" which exist somewhere between single lines of code and programs. Refer to this lecture's recording for discussion.)

Programs do not automatically print anything out, and so we must make use of the print function anywhere we need to see the results.

In [36]:
x = 2 * 3
y = x / 5
# nothing printed out
In [37]:
x = 2 * 3
y = x / 5
print(x, y)
6 1.2

Print¶

The print function comes in a few forms, allowing for combining expressions, strings, and variables in different ways for output. print's arguments will always be some kind of expression -- something evaluating to a value.

In [38]:
print("Hello World")
Hello World
In [39]:
print("42")
42
In [40]:
print(3 + 6)
9
In [41]:
x = 3
y = 5
print("Value of x is", x, "and the value of y is", y)
Value of x is 3 and the value of y is 5
In [42]:
x + "a"
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[42], line 1
----> 1 x + "a"

TypeError: unsupported operand type(s) for +: 'int' and 'str'
In [43]:
str(x)
Out[43]:
'3'
In [44]:
str(x) + "a"
Out[44]:
'3a'
In [2]:
x = 3
y = 5
print("Value of x is " + str(x) + " and the value of y is " + str(y))
Value of x is 3 and the value of y is 5
In [58]:
x = 3
y = 5
print(f"Value of x is {x} and the value of y is {y}")
Value of x is 3 and the value of y is 5
In [48]:
print("What if I wrote a haiku about a print function.")
What if I wrote a haiku about a print function.
In [49]:
print("What if I\nwrote a haiku about a\nprint function.")
What if I
wrote a haiku about a
print function.
In [51]:
print("\\n")
\n
In [54]:
print("Hello", end="")
print("World")
HelloWorld
In [55]:
print("")

In [57]:
print("Hello")
print()
print("World")
Hello

World
In [ ]: