1. Assign the text "Hello World" to a variable named x.
x = "Hello World"
2. Print the value assigned to the variable x.
print(x)
3. Implicity print the value assigned to x.
x
1. Assign a boolean value True to a variable named b.
b = True
2. Print the value assigned to the boolean variable b.
print(b)
3. Assign the integer value 123 to a variable named i.
i = 123
4. Print the value assigned to the variable i.
print(i)
5. Assign the numeric (floating-point) value 2.34 to a variable named f.
f = 2.34
6. Print the value assigned to the variable f.
print(f)
7. Assign the character string "ABC 123" to a variable named c.
c = "ABC 123"
8. Print the value assigned to the variable c.
print(c)
1. Create a function named add
that takes two argument a and b and returns a + b.
def add(a, b):
return a + b
2. Invoke the function with the arguments a = 1 and b = 2.
add(1, 2)
1. Create a tuple named t with the values 1, 2, 3.
t = (1, 2, 3)
2. Print the tuple t.
print(t)
3. Access the first element of the tuple (i.e. index of 0).
t[0]
4. Create a list named l with the values 2, 3, 4.
l = [2, 3, 4]
5. Print the list l.
print(l)
6. Access the second element of the list (i.e. index 1).
l[1]
7. Create a dictionary named d with the values a:1, b:2, c:3.
d = {"a":1, "b":2, "c":3}
8. Print the dictionary d.
print(d)
9. Access the value assigned to the key "c".
d["c"]
1. Import the numpy package as "np".
import numpy as np
2. Create a numpy array named a with the values 1, 2, 3.
a = np.array([1, 2, 3])
3. Print the array a.
print(a)
4. Access the first element of the array a (i.e. index 0).
a[0]
5. Create an array from the sequence of integers 1 through 5.
Note: End-of-range value is exclusive.
s = np.arange(1, 6)
6. Print the sequence of values.
print(s)
7. Create a 2x3 matrix named m containing the values 1-6.
m = np.matrix([[1, 2, 3], [4, 5, 6]])
8. Print the matrix m.
print(m)
9. Access the value contained in the first row and second column of the matrix.
m[0, 1]
1. Import the pandas library as "pd".
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 |
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.
print(df)
4. Index the first row and second column of the data frame df.
df.iloc[0, 1]
5. Index the first row and all columns of the data frame df.
df.iloc[0, :]
6. Index all rows of the second column of the data frame df.
df.iloc[:, 0]
7. Index the second column of data frame df by column name (i.e. "How_Many").
df.loc[:, "How_Many"]
1. Subset the first and third row from the data frame df.
df.iloc[[1, 3], :]
2. Subset rows one through three as a sequence of values.
df.iloc[1:4, :]
3. Subset the first and third row using a list of boolean values.
df.iloc[[True, False, True, False], :]
4. Execute a predicate function returning True where Is_Pet is equal to True.
df.Is_Pet == True
5. Subset all rows in the data frame where Is_Pet is equal to True.
df[df.Is_Pet == True]
6. Subset all rows where How_Many is greater than 12.
df[df.How_Many > 12]
7. Subset all rows where Name is in the list ["Cat", "Cow"].
df[df.Name.isin(["Cat", "Cow"])]
1. Create a 2x2 matrix named m1 with float values 1.0 to 4.0 using named arguments.
m1 = np.matrix(
data = [[1, 2], [3, 4]],
dtype = "float")
2. Create an identical matrix named m2 using ordered arguments.
m2 = np.matrix([[1, 2], [3, 4]], "float")
3. Test the equality of these two matrices.
m1 == m2
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.
help(print)