diff --git a/class1.html b/class1.html index d95ce3b..1f0f631 100644 --- a/class1.html +++ b/class1.html @@ -263,16 +263,16 @@
>>> a = 2
>>> b = 3
->>> print a + b
+>>> print (a + b)
5
>>> c = a + b
->>> print c * 2
+>>> print (c * 2)
10
>>> a = 0
>>> a = a + .5
->>> print a
+>>> print (a)
0.5
>>> a
0.5
@@ -301,18 +301,18 @@ Operators for strings
a = 'Hello '
b = 'World'
c = a + b
-print c
+print (c)
a = "Spam "
b = a * 4
-print b
+print (b)
a = 'spam '
b = 'eggs'
c = a * 4 + b
-print c
+print (c)
type()
is a function. We call it by using parenthesis and pass it an object by placing the object inside the parenthesis
a = 4
-print type(a)
-print type(4)
+print (type(a))
+print (type(4))
-print type(3.14)
+print (type(3.14))
b = 'spam, again'
-print type(b)
-print type("But I don't like spam")
+print (type(b))
+print (type("But I don't like spam"))
->>> print "Spam" / "eggs"
+>>> print ("Spam" / "eggs")
Traceback (most recent call last):
File "", line 1, in
TypeError: unsupported operand type(s) for /: 'str' and 'str'
@@ -377,7 +377,7 @@ Errors - continued ...
# NameError - Using a name that hasn't been defined yet
a = 5
-print b
+print (b)
b = 10
# TypeError - Using an object in a way that its type does not support
@@ -461,7 +461,7 @@ The Text Editor
Click "File", then "Open". Navigate to the gdi-intro-python
folder we just created and click "Open"
In the text editor, enter the following:
-print 'Hello World!'
+print ('Hello World!')
Click "File", then "Save As...". Type "class1.py" and click "Save".
@@ -473,13 +473,13 @@ The Text Editor
User Input
- To obtain user input, use raw_input()
+ To obtain user input, use input()
Change the class1.py text to the following and run it again
-name = raw_input("What is your name?")
-age = raw_input("How old are you?")
+name = input("What is your name?")
+age = input("How old are you?")
age_int = int(age)
-print "Your name is" + name + " and you are " age + " years old."
+print ("Your name is " + name + " and you are " + age + " years old.")
type(age)
type(age_int)
@@ -490,7 +490,7 @@ User Input
Let's Develop It!
- Write your own program that uses raw_input
and does something else with the value
+ Write your own program that uses input
and does something else with the value
You can use float() to treat the input as a number if you need a number,
or use the input directly as a string.
diff --git a/class2.html b/class2.html
index 9ae052c..f3520bf 100644
--- a/class2.html
+++ b/class2.html
@@ -66,10 +66,10 @@ Boolean Expressions
a = 5
b = 5
-print a == b
+print (a == b)
c = 3
-print a == c
-print c < a
+print (a == c)
+print (c < a)
@@ -105,9 +105,9 @@ Boolean Expressions continued
a = 3
b = 4
-print a != b
-print a <= 3
-print a >= 4
+print (a != b)
+print (a <= 3)
+print (a >= 4)
Remember: Equals does not equal "equals equals"
@@ -138,7 +138,7 @@ Appending to list
to_do_list = []
to_do_list.append('buy soy milk')
to_do_list.append('learn python')
-print to_do_list
+print (to_do_list)
Therefore, lists are mutable
This means that a list can change values
during the duration of a program
To index on a list, follow it immediately with [index]
.
numbers = [10, 20, 30]
-print numbers[0]
+print (numbers[0])
Lists (and other data types) start at the number 0 and count up.
@@ -166,13 +166,13 @@An IndexError results if an index exceeds the length of the list minus 1
@@ -196,7 +196,7 @@We achieve this using if statements
if x == 5:
- print 'x is equal to 5'
+ print ('x is equal to 5')
@@ -206,9 +206,9 @@ We often want a different block to execute if the statement is false.
This can be accomplished using else
if x == 5:
- print 'x is equal to 5'
+ print ('x is equal to 5')
else:
- print 'x is not equal to 5'
+ print ('x is not equal to 5')
@@ -220,14 +220,14 @@ Write this into your text editor and save the file as class2.py in your gdi-intro-python folder.
-print "It's your birthday!"
-answer = raw_input("How old are you? ")
+print ("It's your birthday!")
+answer = input("How old are you? ")
age = int(answer)
-if answer < 21:
- print "You may not have a beer, but here's some juice!"
+if age < 21:
+ print ("You may not have a beer, but here's some juice!")
else:
- print "Here's some beer!"
-print "Happy birthday!"
+ print ("Here's some beer!")
+print ("Happy birthday!")
Chained conditionals use elif
as an additonal check after the preceeding if
predicate was False. For example:
if x < 0:
- print "x is negative"
+ print ("x is negative")
elif x > 0:
- print "x is positive"
+ print ("x is positive")
else:
- print "x is 0"
+ print ("x is 0")
Nested conditionals occur inside of other conditionals, and are indented over once more. When the code block is complete you move back.
Edit your python file to contain the following:
-print "It's your birthday!"
-answer = raw_input("How old are you? ")
+print ("It's your birthday!")
+answer = input("How old are you? ")
age = int(answer)
-if answer < 21:
- print "You may not have a beer, but here's some juice!"
+if age < 21:
+ print ("You may not have a beer, but here's some juice!")
else:
- beers = raw_input("How many beers do you want?")
+ beers = input("How many beers do you want?")
beers = int(beers)
if beers > 3:
- print "Oops, you're drunk!"
+ print ("Oops, you're drunk!")
elif beers > 1:
- print "You got a little tipsy"
+ print ("You got a little tipsy")
else:
- print "Looks like you're the designated driver"
-print "Happy birthday!"
+ print ("Looks like you're the designated driver")
+print ("Happy birthday!")
The code below is an example:
health = 100
-print "A vicious warg is chasing you."
-print "Options:"
-print "1 - Hide in the cave."
-print "2 - Climb a tree."
-input_value = raw_input("Enter choice:")
+print ("A vicious warg is chasing you.")
+print ("Options:")
+print ("1 - Hide in the cave.")
+print ("2 - Climb a tree.")
+input_value = input("Enter choice:")
if input_value == '1':
- print 'You hide in a cave.'
- print 'The warg finds you and injures your leg with its claws'
+ print ('You hide in a cave.')
+ print ('The warg finds you and injures your leg with its claws')
health = health - 10
elif input_value == '2':
- print 'You climb a tree.'
- print 'The warg eventually looses interest and wanders off'
+ print ('You climb a tree.')
+ print ('The warg eventually looses interest and wanders off')
else:
- print 'Invalid option.'
+ print ('Invalid option.')
@@ -302,12 +302,12 @@ The repeated execution of a set of statements is called iteration
One way to acheive this, is with the while loop.
-user_input = raw_input('would you like to quit? (y/n) ')
+user_input = input('would you like to quit? (y/n) ')
while user_input != 'y':
- user_input = raw_input('would you like to quit? (y/n) ')
+ user_input = input('would you like to quit? (y/n) ')
-print 'quitters never win!'
+print ('quitters never win!')
As long as the while statement evaluates to True, the code block beneath it is repeated.
@@ -316,12 +316,12 @@Consider the following example that uses iteration with a while loop
-input_value = raw_input('Enter a positive integer:')
+input_value = input('Enter a positive integer:')
user_number = int(input_value)
while user_number > 0:
- print "The user's number is still positive, let's subtract 1"
+ print ("The user's number is still positive, let's subtract 1")
user_number = user_number - 1
-print "User's number is no longer positive."
+print ("User's number is no longer positive.")
This implementation does not work for negative numbers. Why?
@@ -340,34 +340,17 @@What do you think the following functions output?
-
- range(5)
-
-
- range(5, 10)
-
-
- range(10, 20, 2)
-
-
- range(20, 8, -2)
-
- Let's examine the example carefully
for number in range(5):
- print "The current number is:"
- print number
+ print ("The current number is:")
+ print (number)
This for loop has three parts:
for moose in range(5):
- print "The current number is:"
- print moose
+ print ("The current number is:")
+ print (moose)
We have already made a function call
when using the type
, int
, or float
functions
a = '3'
-print type(a)
+print (type(a))
a = float(a)
a = 3
-print type(a)
+print (type(a))
A function can take arguments
In the example above, the variable a
is passed
as an argument to the function type
The following example is a function definition.
This allows us to create our own functions
def print_plus_5(x):
- print x + 5
+ print (x + 5)
The function definition has the following parts
We just used x as our argument, but I can replace that with duck and the function will work the same
def print_plus_5(duck):
- print duck + 5
+ print (duck + 5)
When naming your functions and your arguments, you should use terms based on the task at hand
@@ -125,14 +125,14 @@A function does not have to take arguments,
as in the following example:
def grass():
- print 'The grass is green.'
+ print ('The grass is green.')
grass()
@@ -156,7 +156,7 @@ Function composition is when the output of one function acts as the input of another
def get_name():
- return raw_input("What is your name?")
+ return input("What is your name?")
def get_email_address():
- return raw_input("What is your address?")
+ return input("What is your address?")
def register():
- print "Please fill out the following information to register:"
+ print ("Please fill out the following information to register:")
name = get_name()
email = get_email_address()
- print name, "is registered under the email address", email
+ print (name, "is registered under the email address", email)
If you aren't certain a key is present, you can use the get
method
get
will return None if the key isn't present.
color = [255, 255, 0]
if 0 in color:
- print '0 is in the color'
+ print ('0 is in the color')
menu = {'tofu': 4}
-print 'tofu' in menu
-print 4 in menu
+print ('tofu' in menu)
+print (4 in menu)
@@ -283,10 +283,6 @@ len()
range()
sorted()
# Using len() - Determines length
-print len([1, 2])
-print len("Hello")
-
-# range() - Quickly creates a list of integers
-print range(5)
-print range(5, 10)
-print range(0, 10, 2)
+print (len([1, 2]))
+print (len("Hello"))
# sorted() - Sort a given list
grades = [93, 100, 60]
grades = sorted(grades)
-print grades
+print (grades)
-print 'To Do:'
+print ('To Do:')
to_dos = ['work', 'sleep', 'work']
for item in to_dos:
- print '%s' %(item)
+ print ('%s' %(item))
name = 'Sally'
-print 'Her name is {}'.format(name)
+print ('Her name is {}'.format(name))
>>> list_a = [0, 3, 2, 0, 2, 7]
>>> set_a = set(list_a)
->>> print set_a
+>>> print (set_a)
set([0, 2, 3, 7])
>>> set_b = {1, 2, 1, 3}
->>> print set_b
+>>> print (set_b)
set([1, 2, 3])
Sets have an add method, which like append for lists, adds an element to a set.
@@ -100,11 +100,11 @@What if we want to flatten the family shopping list?
What if we want the food items in the family shopping list to be unique?
@@ -126,9 +126,9 @@One commonly used, higher order function
(that is a Python builtin) is called map
-# Define any function
-def sqaure(number):
- return number ** 2
-# Pass the function to map along with an iterable
-squares = map(square, range(10))
+ # Define any function
+ def square(number):
+ return number ** 2
+
+ # Pass the function to map along with an iterable
+ squares = list(map(square, range(10)))
+
+ print(squares)