Getting started with Python 2
CS 102
Lab 3
Fall 2012
Name: _________________________________ The symbol marks programming exercises. Upon completion, always capture a screenshot and include it in your lab report. Email lab report to instructor at the end of the lab.
Review of built-in functions covered last time: int() and float() help("...")
Review of math functions covered last time: import math math.sqrt() math.pi math.sin(), math.cos(), math.radians() math.floor() and math.ceil()
-------------------------------------------------------------------------------------------------------------------------
Use the Python shell to calculate how many bits are needed to represent each set of things:
o The 3143 counties in the USA
o The 312,931,000 people in the USA1
o The 196 countries of the world
Hint: use the log() function from the math module. If you forgot how to use it, use the help command:
1 2010 census
CS 102 Binary fractions
Lab 3
Fall 2012
Convert these binary fractions to decimal using the negative powers of 2. Show your work!
0.1 =
0.01 =
0.11
0.101 =
Convert these decimal fractions to binary using repeated multiplication. If the fraction is infinite, stop at 8 bits. Show your work!
0.75 = 0.
0.625 = 0.
0.4 = 0.
CS 102
Lab 3 Variables in Python
Fall 2012
What if the value assigned or obtained at one point in our session is needed later for further processing? With the tools we have so far, there's no choice but to re-enter it manually. (Remember how we implemented the repeated division algorithm in the previous lab!)
Python, like any other programming language, allows the user to define variables, which are just places in the computer memory where data is stored.
In Python, variables are declared by simply giving them values, like so:
>>> count = 25
The value is from now on available by calling the variable name:
>>> count
25
If we try to use a variable that has not been initialized, an error occurs ? try it!
Values stored in variables can be modified at any time:
>>> count = 42
It is possible to modify the variable, then reassign the new value to the same variable, all in one Python command. A heavily-used technique is the incrementation of a variable:
>>> count = count + 1
It is possible (but rarely used) to assign several variables in the same Python line:
>>> a, b = 42, 43
Enter in the shell the commands on p.8.
In batch mode, write a program that uses two variables, q (quotient) and r (remainder) to
"automate" the repeated division algorithm from Lab 2.
Test your solution with the numeric example from Ch.2:
CS 102
Lab 3
Fall 2012
Hint: We may use a third variable R to store the base. But we don't need a separate variable for the number to convert (179), since it can be stored directly in q.
Strings of characters in Python Strings are one of Python's collection classes, more precisely a sequential collection class. Read p.9 of the lab manual. Strings can be entered with single, double or triple quotes. Individual characters in a string are numbered from 0 to n-1 and they can be accessed using square brackets. Individual characters cannot be changed!
Enter in interactive mode the commands on p.10 of the Python Manual (no screenshot)
Then do the same thing using your own name, and print the 5th character in it using brackets.
We need not stop at individual characters! Python allows to extract any sub-string using the slicing operator. Just don't forget that in a string of N characters, the first one has index 0, and the last one has index N-1. Referring to index N is illegal!
Read the commands at the bottom of p.11 of the Python Manual. Then place in a string variable
your own full name (including the middle initial2) and use slicing to extract and print the last name, first name, and middle initial.
Sub-strings can be put together with the concatenation operator +, which we encountered in the first lab. This is important, because, as you remember, individual parts of a string cannot be changed in Python. The solution is to create a new string, whose parts come from the old one!
2 Or use a made-up letter.
CS 102
Lab 3
Fall 2012
Use the string with your own full name (declared before) to create a new string, with the format
'Last_name, middle_name middle_initial.'
E.g. 'Jane A. Doe' becomes 'Doe, Jane A.'
Also available for strings (any any other sequential data type in Python), are the operators:
Repetition *
Length
len
Membership in
Finally, it it possible to iterate (repeat a certain action) over each character in the string, using the for command.
In interactive mode, enter the commands at the top of p.11, and the ones on p.12 (no screenshot) In a batch file, enter commands to do the following:
Assign 'tic-tac-' to the variable str1 Use the repetition operator * to assign ten 'tic-tac-'s to the variable str2 Print str1 and str2 Print the length of str2 Test if the sub-string 'ic' is in str2 Print the characters in str1 one at a time, using for
For an overview, consult Table 1 on p.10. Copy Table 1 in your cheat-sheet. Today we've covered pp.7-12 of the Python manual.
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related searches
- getting started in mutual funds
- getting started with minecraft
- getting started with minecraft pi
- getting started with mutual funds
- minecraft getting started guide
- getting started in minecraft xbox
- getting started with amazon fba
- salesforce getting started workbook
- getting started in minecraft
- salesforce getting started guide
- getting started with youtube
- getting started on ebay selling