﻿

CSC 241 Lab 2

Submission:  Submit the python or text file that contains all your answers to the submission folder for this lab by the end of the lab. To simplify Drew's (our tutor) work, please include your name and the number of the lab at the top of the file (if you're submitting a Python file, use comments: start the line with a #).

1. (Variables, and strings)

1. Assign x to be 5, y to be 10, and z 8. Write an expression for the average of x, y and z.

2. How many letters are there in 'Supercalifragilisticexpialidocious'?

3. Store your first name in the string fname (e.g. 'Marcus'), and your last name (e.g. 'Schaefer') in the string lname. Write an expression that displays the name in the form "Schaefer, Marcus' (including punctuation and space). Hint: use string concatenation.

4. Store your first name in the string fname (e.g. 'Marcus'), your last name (e.g. 'Schaefer') in the string lname and your middle name (e.g. 'Georg') in the variable mname. Write an expression that displays the name in the form "Schaefer, Marcus G', so only using the middle initial. You can assume that the person has a middle name.

2. (Truth)

1. Write an expression that checks whether two variables (say x and y) are different. (E.g. x = 3, y = 5).

2. Write an expression that checks whether three variables (say x, y, and z) are all three different (E.g. true for x =3, y = 5, z = 1; but not for x = 1, y = 3, z = 1).

3. Write an expression that checks whether a string s contains the string 'the', either upper, or lower case. So positive instances are s = 'theater', 'bathes', 'In The Loop', negative instances are 'hello world', 'thanks'. Hint: use logic, if you know about .lower(), don't use it.

4. Write an expression that checks whether x is smaller than -32768 or larger than 32767. Test with x = -40000, x = 10, x = 40000.

5. Write an expression that checks whether x is an integer smaller than -32768 or larger than 32767. Hint: there are two parts, the range, and the integer check. You already did the range check in d, now add the integer check.  Hint 2: Use x == x//1 to test whether x is an integer. Hint 3: yes, worry about the parentheses. Check with x = -40000, x = -40000.5 x = 10, x = 40000, x = 40000.5.

3. (Lists)

1. Write an expression that computes the average of a list lst. Hint: work with the sum and the len functions.

2. Write an expression that returns the first half of the list lst. Note: for a list with an odd number of elements, you can include the middle element, or not, whichever is easier (not including it is easier). Hint: remember // and len. Marcus Schaefer
Last updated: April 8th,2018.