You are on page 1of 3

Assignment1

Part A

1. Given: colors = ['Red', 'Blue', 'Green', 'Black', 'White']

Using Python method of accessing elements from a list to:

Get the first element from the list

Get the second element from the list

Get the last element from the list

Get the last three elements from the list

Get the middle three (3) element of the list

Replace the first color from the list with ‘Yellow’

Replace the last color from the list with ‘Red’

2. Use a List Comprehension to generate the square of numbers from 1 to 100.


3. Write Python codes to print numbers 0 to 1000001
4. Create a program that asks the user to enter their name and their age. Print out a
message addressed to them that tells them the year that they will turn 100 years old.
5. Let’s say I give you a list saved in a variable: myList = [1, 4, 9, 16, 25, 36, 49, 64, 81,
100]. Write one line of Python that takes this list myList and makes a new list that has
only the even elements of this list in it.
6. Given:
years_of_birth = [1990, 1991, 1990, 1990, 1992, 1991]
ages = [2014 - year for year in years_of_birth]
print out the output.

7. Ask the user for a string and print out whether this string is a palindrome or not. (A
palindrome is a string that reads the same forwards and backwards.)
8. Ask the user for a number. Depending on whether the number is even or odd, print out
an appropriate message to the user, e.g, “The number just entered is even” if the
number is an even number, and vice versa.

Part B

1. What is a variable?

2. What are the primitive built-in types in Python?

3. When should we use “”” (triple quotes) to define strings?


4. Give name = “John Smith”

What does name[1] return?

What about name[-2]?

What about name[1:-1]?

How to get the length of name?

5. What are the escape sequences in Python? Give an example of how each of them is
used.

6. What is the result of f“{2+2}+{10%3}”?

7. Given name = “john smith”

What will name.title() return?

What does name.strip() do?

What will name.find(“Smith”) return?

What will be the value of name after we call name.replace(“j”, “k”)?

How can we check to see if name contains “John”?

8. What are the 3 types of numbers in Python?

Part C

1. What is the difference between 10 / 3 and 10 // 3?


2. What is the result of 10 ** 3?
3. Given (x = 1), what will be the value of after we run (x += 2)?
4. How can we round a number?
5. What is the result of float(1)?
6. What is the result of bool(“False”)?
7. What are the falsy values in Python?
8. What is the result of 10 == “10”?
9. What is the result of “bag” > “apple”?
10. What is the result of not(True or False)?
11. Under what circumstances does the expression 18 <= age < 65 evaluate to True?
12. What does range(1, 10, 2) return?
13. Name 3 iterable objects in Python.
14. What is a Python expression?
15. What is a syntax error?
16. What is the result of this expression: “*” * 10
Part D

Answer exercises 1&2 on page 20 of the Second Handout

You might also like