From c9725ec58927763dd1392f05b318ecb3a636cec1 Mon Sep 17 00:00:00 2001
From: Kamilla Khabibrakhmanova Remember: Equals does not equal "equals equals" Therefore, lists are mutable This means that a list can change values To index on a list, follow it immediately with Lists (and other data types) start at the number 0 and count up. An IndexError results if an index exceeds the length of the list minus 1 We achieve this using if statements We often want a different block to execute if the statement is false. Write this into your text editor and save the file as class2.py in your gdi-intro-python folder.
Chained conditionals use 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: The code below is an example: As long as the while statement evaluates to True, the code block beneath it is repeated. This implementation does not work for negative numbers. Why? Let's examine the example carefully This for loop has three parts:Variables and Arithmetic
>>> 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)
Data Types
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"))
Data types - continued ...
->>> print "Spam" / "eggs"
+>>> print ("Spam" / "eggs")
Traceback (most recent call last):
File "
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
gdi-intro-python
folder we just created and click "Open"
-print 'Hello World!'
+print ('Hello World!')
User Input
name = raw_input("What is your name?")
age = raw_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)
diff --git a/class2.html b/class2.html
index 9ae052c..5a2d066 100644
--- a/class2.html
+++ b/class2.html
@@ -66,10 +66,10 @@ Boolean Expressions
@@ -105,9 +105,9 @@
a = 5
b = 5
-print a == b
+print (a == b)
c = 3
-print a == c
-print c < a
+print (a == c)
+print (c < a)
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)
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)
during the duration of a programIndexing
[index]
.
numbers = [10, 20, 30]
-print numbers[0]
+print (numbers[0])
Length and More Indexing
to_do_list = [
'learn python', 'read email', 'make lunch',
]
-print len(to_do_list)
+print (len(to_do_list))
-print to_do_list[0]
-print to_do_list[2]
+print (to_do_list[0])
+print (to_do_list[2])
-print to_do_list[len(to_do_list)]
-print to_do_list[len(to_do_list) - 1]
+print (to_do_list[len(to_do_list)])
+print (to_do_list[len(to_do_list) - 1])
Conditionals
@@ -206,9 +206,9 @@
if x == 5:
- print 'x is equal to 5'
+ print ('x is equal to 5')
Conditionals
This can be accomplished using else
@@ -220,14 +220,14 @@
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')
Indentation
-print "It's your birthday!"
+print ("It's your birthday!")
answer = raw_input("How old are you? ")
age = int(answer)
if answer < 21:
- print "You may not have a beer, but here's some juice!"
+ 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
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
-print "It's your birthday!"
+print ("It's your birthday!")
answer = raw_input("How old are you? ")
age = int(answer)
if answer < 21:
- print "You may not have a beer, but here's some juice!"
+ print ("You may not have a beer, but here's some juice!")
else:
beers = raw_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!")
Let's Develop It
@@ -307,7 +307,7 @@
health = 100
-print "A vicious warg is chasing you."
-print "Options:"
-print "1 - Hide in the cave."
-print "2 - Climb a tree."
+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:")
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.')
Iteration
while user_input != 'y':
user_input = raw_input('would you like to quit? (y/n) ')
-print 'quitters never win!'
+print ('quitters never win!')
While loops
input_value = raw_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.")
For loops
for price in prices:
costs.append(price + shipping_cost)
-print costs
+print (costs)
@@ -366,8 +366,8 @@ For loops continued
for number in range(5):
- print "The current number is:"
- print number
+ print ("The current number is:")
+ print (number)
@@ -385,8 +385,8 @@
Variable name in for loops
processing, not understanding.
for moose in range(5):
- print "The current number is:"
- print moose
+ print ("The current number is:")
+ print (moose)
diff --git a/class3.html b/class3.html
index 1c6210e..f26811d 100644
--- a/class3.html
+++ b/class3.html
@@ -64,7 +64,7 @@ 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 @@ 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)
@@ -302,31 +302,31 @@
# Using len() - Determines length
-print len([1, 2])
-print len("Hello")
+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 (range(5))
+print (range(5, 10))
+print (range(0, 10, 2))
# 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 @@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.")
@@ -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 5a2d066..ca8ce0d 100644
--- a/class2.html
+++ b/class2.html
@@ -221,7 +221,7 @@ Indentation
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? ")
+answer = input("How old are you? ")
age = int(answer)
if answer < 21:
print ("You may not have a beer, but here's some juice!")
@@ -252,12 +252,12 @@ Nested conditionals
Edit your python file to contain the following:
print ("It's your birthday!")
-answer = raw_input("How old are you? ")
+answer = input("How old are you? ")
age = int(answer)
if answer < 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!")
@@ -280,7 +280,7 @@ Let's Develop It
print ("Options:")
print ("1 - Hide in the cave.")
print ("2 - Climb a tree.")
-input_value = raw_input("Enter choice:")
+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')
@@ -302,10 +302,10 @@ Iteration
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!')
@@ -316,7 +316,7 @@ Iteration
While loops
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")
diff --git a/class3.html b/class3.html
index f26811d..9c674e4 100644
--- a/class3.html
+++ b/class3.html
@@ -181,10 +181,10 @@ Function Composition
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:")
From f6b74f0d07e44bac1b083ee2db58d5228f940d0a Mon Sep 17 00:00:00 2001
From: Kamilla Khabibrakhmanova
Date: Sun, 11 Nov 2018 18:06:49 -0300
Subject: [PATCH 3/9] typo
---
class1.html | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/class1.html b/class1.html
index 164a115..1f0f631 100644
--- a/class1.html
+++ b/class1.html
@@ -479,7 +479,7 @@ User Input
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)
From 6504224b88d0a389528d8210de8c96749ad91271 Mon Sep 17 00:00:00 2001
From: Kamilla Khabibrakhmanova
Date: Sun, 11 Nov 2018 22:53:02 -0300
Subject: [PATCH 4/9] another typo
---
class2.html | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/class2.html b/class2.html
index ca8ce0d..c365a12 100644
--- a/class2.html
+++ b/class2.html
@@ -223,7 +223,7 @@ Indentation
print ("It's your birthday!")
answer = input("How old are you? ")
age = int(answer)
-if answer < 21:
+if age < 21:
print ("You may not have a beer, but here's some juice!")
else:
print ("Here's some beer!")
From e1d5964f5813499621e6f812060c9e486234a69d Mon Sep 17 00:00:00 2001
From: Kamilla Khabibrakhmanova
Date: Sun, 11 Nov 2018 22:55:02 -0300
Subject: [PATCH 5/9] same typo as the last one
---
class2.html | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/class2.html b/class2.html
index c365a12..831311b 100644
--- a/class2.html
+++ b/class2.html
@@ -254,7 +254,7 @@ Nested conditionals
print ("It's your birthday!")
answer = input("How old are you? ")
age = int(answer)
-if answer < 21:
+if age < 21:
print ("You may not have a beer, but here's some juice!")
else:
beers = input("How many beers do you want?")
From 728fbd76722216bbb37358b68729670d52c675a4 Mon Sep 17 00:00:00 2001
From: Kamilla Khabibrakhmanova
Date: Sun, 11 Nov 2018 22:58:48 -0300
Subject: [PATCH 6/9] remove range() slide because it is weird in python3
---
class2.html | 17 -----------------
1 file changed, 17 deletions(-)
diff --git a/class2.html b/class2.html
index 831311b..f3520bf 100644
--- a/class2.html
+++ b/class2.html
@@ -344,23 +344,6 @@ For loops
-
- Range
- What do you think the following functions output?
-
- range(5)
-
-
- range(5, 10)
-
-
- range(10, 20, 2)
-
-
- range(20, 8, -2)
-
-
-
For loops continued
Let's examine the example carefully
From 0df21058597cee94ecce3c03b3a01b77455b790d Mon Sep 17 00:00:00 2001
From: Kamilla Khabibrakhmanova
Date: Sun, 11 Nov 2018 23:17:03 -0300
Subject: [PATCH 7/9] remove range again
---
class3.html | 4 ----
1 file changed, 4 deletions(-)
diff --git a/class3.html b/class3.html
index 9c674e4..14cd80f 100644
--- a/class3.html
+++ b/class3.html
@@ -283,10 +283,6 @@ Builtins for data types
len()
Given a data type, return its length
-
- range()
- Create a list of integers in the range provided.
-
sorted()
Given a data type, returns a sorted copy of that data type
From fc97b27c2013bb96367f8d62772176fc2f818745 Mon Sep 17 00:00:00 2001
From: Kamilla Khabibrakhmanova
Date: Sun, 11 Nov 2018 23:18:19 -0300
Subject: [PATCH 8/9] more range removal
---
class3.html | 5 -----
1 file changed, 5 deletions(-)
diff --git a/class3.html b/class3.html
index 14cd80f..1000809 100644
--- a/class3.html
+++ b/class3.html
@@ -301,11 +301,6 @@ Examples of using Builtins
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))
-
# sorted() - Sort a given list
grades = [93, 100, 60]
grades = sorted(grades)
From f23d93b6bae6eedfc64d7c8afd53bc5761092833 Mon Sep 17 00:00:00 2001
From: Kamilla Khabibrakhmanova
Date: Sun, 11 Nov 2018 23:23:34 -0300
Subject: [PATCH 9/9] fix map for python3
---
class4.html | 13 ++++++++-----
1 file changed, 8 insertions(+), 5 deletions(-)
diff --git a/class4.html b/class4.html
index 43bd4dd..a9a9b08 100644
--- a/class4.html
+++ b/class4.html
@@ -207,12 +207,15 @@ Functional Python: Map
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)