Introduction to Python
davidbpython.com
In-Class Exercises, Session 1
PLEASE REFER to pythonreference.com for syntax to follow in coding these Exercises |
|
ALSO PLEASE NOTE THAT THESE ARE NOT HOMEWORK PROBLEMS TO BE TURNED IN! The homework projects are linked as 'Homework Projects' at the bottom of the Session's links. |
|
VARIABLES AND ASSIGNMENT; IDENTIFYING OBJECT TYPE |
|
Ex. 1.1 | Numeric and string assignment, and type() function. |
Assign an integer to new variable 'vari', a float to 'varf' and a string to 'varstr'. Print, and use the type() function to print the value and type of one or more of them. |
|
MATH AND STRING OPERATORS |
|
Ex. 1.2 | Math operators. |
Add 'a' to 'b', multiply by 'c' and divide by 'd' (result should be 10.0). |
|
a = 5
b = 10
c = 2
d = 3
|
|
Keep in mind that since multiplication and division are always evaluated before addition or subtraction, you must do these operations in separate statements, or use parentheses to enforce precedence. |
|
Ex. 1.3 | Exponentiation operator. |
Use '**' to raise a to the power of b (result should be 125). |
|
a = 5
b = 3
|
|
Ex. 1.4 | Concatenation operator. |
Use '+' to concatenate xvar, yvar and zvar together. |
|
xvar = 'supercali'
yvar = 'fragilistic'
zvar = 'expialidocious'
|
|
Ex. 1.5 | String repetition operator. |
Use '*' to repeat the string in greet 3 times to return a new str value yoyoyo. |
|
greet = 'yo'
|
|
LAB 1 |
|
Ex. 1.6 | Square a number. |
gg = 5
|
|
Expected Output:
25 |
|
Please note import runreport refers to my own custom module for working with the class during lab periods. For now it is disabled, we will discussed when we get an opportunity. |
|
Ex. 1.7 | Multiply 2 numbers, then square the result. |
xx = 5
yy = 3
|
|
Expected Output:
225 |
|
Note that the exponientation operator has greater precedence than the multiplication operator, so it is evaluated first. You must use parentheses to enforce a different precendence. Please also note import runreport refers to my own custom module for working with the class during lab periods. For now it is disabled, we will discussed when we get an opportunity. |
|
Ex. 1.8 | Add two numbers together and then divide by 2. |
aa = 3
myb = 5
|
|
Expected Output:
4.0 |
|
Consider that the division operator gets evaluated before the plus operator. If you are calculating both in the same statement, use parentheses to cause Python to evaluate the plus first. Please note import runreport refers to my own custom module for working with the class during lab periods. For now it is disabled, we will discussed when we get an opportunity. |
|
Ex. 1.9 | Calculate a tip. |
With a bill of $100, 5 people in your party and a 20% tip, how much does each person owe? |
|
bill = 100
people = 5
tip_pct = 20
|
|
Expected Output:
24.0 |
|
Please note import runreport refers to my own custom module for working with the class during lab periods. For now it is disabled, we will discussed when we get an opportunity. |
|
Ex. 1.10 | Concatenate strings together into a sentence. |
See if you can make the output look as expected. |
|
adjective = 'happy'
event = 'birthday'
|
|
Expected Output:
happy birthday |
|
Please note import runreport refers to my own custom module for working with the class during lab periods. For now it is disabled, we will discussed when we get an opportunity. |
|
Ex. 1.11 | Use the variable 'x' below to make a 40-character dotted line. |
x = '-'
|
|
Expected Output:
---------------------------------------- |
|
"PROCESSING" FUNCTIONS len() and round() |
|
Ex. 1.12 | Get the length of strings with len(). |
Print the length of each of the below strings. Both strings should be length of 11. |
|
x = 'hello there'
y = 'hi ok'
|
|
Expected Output:
11 11 |
|
Ex. 1.13 | Round numbers with round(). |
Round the number to two places (43.99), and to an int (44). |
|
x = 43.986
|
|
Expected Output:
43.99 44 |
|
"ACTION" FUNCTIONS INPUT, PRINT, EXIT |
|
Ex. 1.14 | Take user input from the keyboard with input(). |
Take keyboard input with input(). Print the result and its type. |
|
|
|
Ex. 1.15 | Use exit() to terminate program execution. |
Imagine we want to test the value of mysum without seeing 'program continues...' (which represents the rest of the code executing). Insert a print() statement and exit() so that the program prints mysum and nothing further. |
|
mysum = (5 + 15) * 3
print('program continues...')
|
|
Expected Output:
60 |
|
Note that in Jupyter Notebook, the notebook will not exit, but will show that exit() has been called. exit() is more appropriate for standalone programs. |
|
"TYPE CONVERSION" FUNCTIONS INT, FLOAT, STR |
|
Ex. 1.16 | int(), float() and str() functions. |
Convert the below string to an integer, then double the value (to 106). Convert this value to a float (106.0) and convert that value to a string. Retrieve and print the length of this string (should be 5). |
|
zzy = '53'
|
|
Ex. 1.17 | Erroneously using built-in names as variables. |
Demo: run this program. Note that the length of the name name is 5 (the length of 'Guido'). Next, add a new int variable: len = 5. Following this, attempt to determine the length of name a second time. What happens? |
|
name = 'Guido'
print(len(name)) # 5
len = 100
# here, please determine the length of <B>name</B> a second time
|
|
REPLICATING CODE EXAMPLES |
|
Ex. 1.18 | Replicating code examples (1/2). |
Review pythonreference.com for the function len(). Use len() to check the length of the variable sentence (length should be 3). Next, use len() to check the length of the literal string value 'sentence' (length should be 8). |
|
sentence = 'hi!'
|
|
Please note that variable sentence (no quotes) is entirely different from string 'sentence' (with quotes). A word without quotes is a variable (a value assigned to a name). A word with quotes around it is a string literal (a literal string value). |
|
Ex. 1.19 | Replicating code examples (2/2). |
Review pythonreference.com for the function round(). Perform operations listed below: |
|
myfloat = 3.927
# round myfloat to 2 decimal places (should be 3.93).
# round myfloat to 0 places (i.e., to produce an integer - should be 4)
|
|
LAB 2 |
|
Ex. 1.20 | Get the character length of the preamble to the constitution. |
preamble = """We the People of the United States, in Order to form a more
perfect Union, establish Justice, insure domestic Tranquility, provide for
the common defense, promote the general Welfare, and secure the Blessings
of Liberty to ourselves and our Posterity, do ordain and establish
this Constitution for the United States of America."""
|
|
Expected Output:
327 |
|
Ex. 1.21 | Divide integers 5 by 3 and round the result to 2 places. |
xx = 5
goofy = 3
|
|
Expected Output:
1.67 |
|
Ex. 1.22 | Take input for name and print 'hello, [name]!'. |
name = input('please enter your name: ')
|
|
Expected Output:
Hello, David! (assumes the user typed David) |
|
Ex. 1.23 | Take input and print the user's value 3 times as a single string. |
val = input('please type anything: ')
|
|
Expected Output:
spamspamspam (assumes the user typed spam) |
|
Ex. 1.24 | Take input for a number and double it. |
x = input('Please enter a number: ')
|
|
Expected Output:
20 (assumes the user typed 10) |
|
Ex. 1.25 | Get the length of 1000000. |
n = 1000000
|
|
Expected Output:
7 |
|
Ex. 1.26 | Take two integers and concatenate them as string values. |
xx = 65
yy = 43
|
|
Expected Output:
6543 |
|
Ex. 1.27 | Take an integer and repeat it three times as a string value. |
yxy = 500
|
|
Expected Output:
500500500 |
|
Ex. 1.28 | Take string input for a float value and round it as a float. Round the user's "float" input to 2 places. |
ui = input('please enter a floating-point number: ')
|
|
Expected Output:
3.93 (assumes user typed 3.926) |
|