Time Module

Provides a number of functions to deal with dates and the time. time.time() Returns number of seconds since January 1, 1970 at 00:00:00 Am. Using time.time() import time max = 10000 t1 = time.time() for x in range(0,max):     print(x) t2 = time.time() total_time = t2-t1 print(“It took”,total_time,”seconds”) The output will look like: It took…

Read More

Random Number Module

Allows you to get a random number generated by the computer. You access it by: import random Most useful functions in the module: randint() – picks a random number from within a range choice() – pick a random item from a list shuffle() – shuffles the list How to use the radint() function This function…

Read More

Using While Loops

When you don’t know how many times you want to repeat an action – while loops come in handy.  They will keep repeating until a condition is met. Here is an example of one that will keep printing a statement until the user replies “n” # first set the variable answer equal to yes answer…

Read More

Homework: For Loops

Boring but useful Create a file called for.py in PythonAnywhere and add these features to it: Create a loop to print out all the even numbers between 1 and 20. Create a loop to print out all the letters in the user’s favorite greeting.  Which means ask the user for their favorite greeting. Create a…

Read More

Homework for Tuples

Create file called tuples.py Create a tuple that has the following information for each class name, teacher, grade make at least three classes display the tuple by looping through it. Create a tuple that has up to 10 values – either numbers or characters or both. Print out the 5-8th elements. Print out the last…

Read More

Dictionaries

Also called a map – these are similar to lists and tuples- in that they are a collection of things.  The difference is that each item in a map has a key and a corresponding value. To create a dictionary: capitals = {‘California’:’Sacramento’, ‘Georgia’:’Atlanta’, ‘Virginia’:’Richmond’ , ‘Maryland’:’Annapolis’} print(capitals) Output {‘California’:’Sacramento’, ‘Georgia’:’Atlanta’, ‘Virginia’:’Richmond’,’Maryland’:’Annapolis’} Colons are used…

Read More

Basics of Tuples

Tuples are very similar to lists – except You can’t change the values that are stored in them. Create them using curved brackets or parentheses like () not square brackets like [] as used in lists. Why use a tuple instead of a list? In case you wanted to make sure it is never changed!…

Read More

Homework: First python program!

Wrap Up In this lesson we have covered the following topics: Intro to the print() statement. How to use white space – but we will have to learn more Python before we can use this. How to do single line and multiple line comments. How to create and assign variables. What types of data you…

Read More

How to Use Python Anywhere

PythonAnywhere is a cloud editor – similar to CodeAnywhere – but one that is created just for Python.  We’ll be using it for this course. Let’s get started. Go to www.pythonanywhere.com in your browser Click in the log in link in the upper right corner. User name is firstnamelastinitial so sophief or lilyc Password is…

Read More

Part 4: Add a Person Class

Person Class Next we’ll create a Person class Attributes Name Job Bank Account Savings Goal Methods GetPaid DepositPay  – 1/3 to savings, 2/3 to checking LengthofTimeReachSavingsGoal

Read More