# Lab 1A- Language Basics¶

### 1. Hello World¶

1. Assign the text "Hello World" to a variable named x.

In [1]:
x = "Hello World"


2. Print the value assigned to the variable x.

In [2]:
print(x)

Hello World


3. Implicity print the value assigned to x.

In [3]:
x

Out[3]:
'Hello World'

### 2. Data Types¶

1. Assign a boolean value True to a variable named b.

In [4]:
b = True


2. Print the value assigned to the boolean variable b.

In [5]:
print(b)

True


3. Assign the integer value 123 to a variable named i.

In [6]:
i = 123


4. Print the value assigned to the variable i.

In [7]:
print(i)

123


5. Assign the numeric (floating-point) value 2.34 to a variable named f.

In [8]:
f = 2.34


6. Print the value assigned to the variable f.

In [9]:
print(f)

2.34


7. Assign the character string "ABC 123" to a variable named c.

In [10]:
c = "ABC 123"


8. Print the value assigned to the variable c.

In [11]:
print(c)

ABC 123


### 3. Functions¶

1. Create a function named add that takes two argument a and b and returns a + b.

In [12]:
def add(a, b):
return a + b


2. Invoke the function with the arguments a = 1 and b = 2.

In [13]:
add(1, 2)

Out[13]:
3

### 4. Data Structures¶

1. Create a tuple named t with the values 1, 2, 3.

In [14]:
t = (1, 2, 3)


2. Print the tuple t.

In [15]:
print(t)

(1, 2, 3)


3. Access the first element of the tuple (i.e. index of 0).

In [16]:
t[0]

Out[16]:
1

4. Create a list named l with the values 2, 3, 4.

In [17]:
l = [2, 3, 4]


5. Print the list l.

In [18]:
print(l)

[2, 3, 4]


6. Access the second element of the list (i.e. index 1).

In [19]:
l[1]

Out[19]:
3

7. Create a dictionary named d with the values a:1, b:2, c:3.

In [20]:
d = {"a":1, "b":2, "c":3}


8. Print the dictionary d.

In [21]:
print(d)

{'a': 1, 'b': 2, 'c': 3}


9. Access the value assigned to the key "c".

In [22]:
d["c"]

Out[22]:
3

### 4. Numpy Data Structures¶

1. Import the numpy package as "np".

In [23]:
import numpy as np


2. Create a numpy array named a with the values 1, 2, 3.

In [24]:
a = np.array([1, 2, 3])


3. Print the array a.

In [25]:
print(a)

[1 2 3]


4. Access the first element of the array a (i.e. index 0).

In [26]:
a[0]

Out[26]:
1

5. Create an array from the sequence of integers 1 through 5.
Note: End-of-range value is exclusive.

In [27]:
s = np.arange(1, 6)


6. Print the sequence of values.

In [28]:
print(s)

[1 2 3 4 5]


7. Create a 2x3 matrix named m containing the values 1-6.

In [29]:
m = np.matrix([[1, 2, 3], [4, 5, 6]])


8. Print the matrix m.

In [30]:
print(m)

[[1 2 3]
[4 5 6]]


9. Access the value contained in the first row and second column of the matrix.

In [31]:
m[0, 1]

Out[31]:
2

### 5. Pandas Data Frames¶

1. Import the pandas library as "pd".

In [32]:
import pandas as pd


2. Create a data frame named df with the following data:

Name How_Many Is_Pet
Cat 5 True
Dog 10 True
Cow 15 False
Pig 20 False
In [33]:
df = pd.DataFrame(
columns = ["Name", "How_Many", "Is_Pet"],
data = [["Cat", 5, True],
["Dog", 10, True],
["Cow", 15, False],
["Pig", 20, False]])


3. Print the data frame df.

In [34]:
print(df)

  Name  How_Many  Is_Pet
0  Cat         5    True
1  Dog        10    True
2  Cow        15   False
3  Pig        20   False


4. Index the first row and second column of the data frame df.

In [35]:
df.iloc[0, 1]

Out[35]:
5

5. Index the first row and all columns of the data frame df.

In [36]:
df.iloc[0, :]

Out[36]:
Name         Cat
How_Many       5
Is_Pet      True
Name: 0, dtype: object

6. Index all rows of the second column of the data frame df.

In [37]:
df.iloc[:, 0]

Out[37]:
0    Cat
1    Dog
2    Cow
3    Pig
Name: Name, dtype: object

7. Index the second column of data frame df by column name (i.e. "How_Many").

In [38]:
df.loc[:, "How_Many"]

Out[38]:
0     5
1    10
2    15
3    20
Name: How_Many, dtype: int64

### 6. Subsetting a Data Frame¶

1. Subset the first and third row from the data frame df.

In [39]:
df.iloc[[1, 3], :]

Out[39]:
Name How_Many Is_Pet
1 Dog 10 True
3 Pig 20 False

2. Subset rows one through three as a sequence of values.

In [40]:
df.iloc[1:4, :]

Out[40]:
Name How_Many Is_Pet
1 Dog 10 True
2 Cow 15 False
3 Pig 20 False

3. Subset the first and third row using a list of boolean values.

In [41]:
df.iloc[[True, False, True, False], :]

Out[41]:
Name How_Many Is_Pet
0 Cat 5 True
2 Cow 15 False

4. Execute a predicate function returning True where Is_Pet is equal to True.

In [42]:
df.Is_Pet == True

Out[42]:
0     True
1     True
2    False
3    False
Name: Is_Pet, dtype: bool

5. Subset all rows in the data frame where Is_Pet is equal to True.

In [43]:
df[df.Is_Pet == True]

Out[43]:
Name How_Many Is_Pet
0 Cat 5 True
1 Dog 10 True

6. Subset all rows where How_Many is greater than 12.

In [44]:
df[df.How_Many > 12]

Out[44]:
Name How_Many Is_Pet
2 Cow 15 False
3 Pig 20 False

7. Subset all rows where Name is in the list ["Cat", "Cow"].

In [45]:
df[df.Name.isin(["Cat", "Cow"])]

Out[45]:
Name How_Many Is_Pet
0 Cat 5 True
2 Cow 15 False

### 7. Miscellaneous Topics¶

1. Create a 2x2 matrix named m1 with float values 1.0 to 4.0 using named arguments.

In [46]:
m1 = np.matrix(
data = [[1, 2], [3, 4]],
dtype = "float")


2. Create an identical matrix named m2 using ordered arguments.

In [47]:
m2 = np.matrix([[1, 2], [3, 4]], "float")


3. Test the equality of these two matrices.

In [48]:
m1 == m2

Out[48]:
matrix([[ True,  True],
[ True,  True]])

4. Question: Why did this return a matrix rather than just a single value "True"?

5. Open the help documentation on Python's print command.

In [49]:
help(print)

Help on built-in function print in module builtins:

print(...)
print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)

Prints the values to a stream, or to sys.stdout by default.
Optional keyword arguments:
file:  a file-like object (stream); defaults to the current sys.stdout.
sep:   string inserted between values, default a space.
end:   string appended after the last value, default a newline.
flush: whether to forcibly flush the stream.