Python. What is Python? A programming language we can use to communicate with the computer and solve...
-
Upload
beatrice-watson -
Category
Documents
-
view
225 -
download
3
Transcript of Python. What is Python? A programming language we can use to communicate with the computer and solve...
![Page 1: Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649db55503460f94aa6793/html5/thumbnails/1.jpg)
Python
![Page 2: Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649db55503460f94aa6793/html5/thumbnails/2.jpg)
What is Python?
• A programming language we can use
to communicate with the computer
and solve problems
• We give the computer instructions
that it will execute
![Page 3: Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649db55503460f94aa6793/html5/thumbnails/3.jpg)
What is Python?
• Python is a high level language that we
(humans) can understand
– Other examples include C++ and Java
• A machine can only understand low level
language
• So we process the high level to low level so
the computer will understand!
![Page 4: Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649db55503460f94aa6793/html5/thumbnails/4.jpg)
Translation• How do we translate high level
languages to low level languages?
–We use interpreters and compilers!
– Interpreters process the program a little
bit at a time and runs it
– Compilers translate everything before
running it
![Page 5: Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649db55503460f94aa6793/html5/thumbnails/5.jpg)
Some Python Vocab
• Program – a file of code that may contain functions
• Script – a short code that we can run in a
command line
• Variable – names we assign the values to, allowing
us to reuse them later on
– For example: x = 1 or msg = “Hello world!”
– Variables can be changed throughout a program
– For example: x = 1, x = x + 1
![Page 6: Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649db55503460f94aa6793/html5/thumbnails/6.jpg)
Python Vocab• Comments – notes ignored by the
computer
– For example: x + y # variables
store user input
• Operators – mathematical symbols
–+, -, *, /, ** (exponents), ==
(equality)
![Page 7: Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649db55503460f94aa6793/html5/thumbnails/7.jpg)
Python Vocab
• Keyword – words with meaning/purpose in
Python
– For example: “and”, “print”, “if”
• Expression – statements that produce values
– 3 + 5, “Hello world!”
• Error – program has a problem in the command
area
• Instance – one run-through of a program
![Page 8: Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649db55503460f94aa6793/html5/thumbnails/8.jpg)
Indentation
• A REQUIREMENT IN PYTHON!
• Indenting specifies the “scope” of
different chunks of your code
– Everything indented after a first,
unindented line “belongs” to that line!
![Page 9: Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649db55503460f94aa6793/html5/thumbnails/9.jpg)
Things to Note• Python is case sensitive
– A function called “first” is different than a function called “FIRST”
or “First” or “fiRSt”
• Python doesn’t like spaces or punctuation marks
– You can’t name your function “spam Five” or “spam.Five”
– You could, however, name your function“spamFive” or “spam_Five”
• Some words in Python can’t be used as names
– Keywords can never be used as function/variable names
– Check the colors of the words! Purple and orange are
KEYWORDS!
![Page 10: Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649db55503460f94aa6793/html5/thumbnails/10.jpg)
LET’S PLAY WITH PYTHON!
![Page 11: Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649db55503460f94aa6793/html5/thumbnails/11.jpg)
Our age(number) function
• We had an argument passed into our
function!
• The argument is known as a parameter
• Example
def add(a, b):
print(“This is a + b: “, a+b)
• a and b are the parameters
![Page 12: Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649db55503460f94aa6793/html5/thumbnails/12.jpg)
Data Types
• Numeric
– Integers (5, 2, -1)
– Floating Point Numbers (0.2, 3.14159, 28.92)
• Non-numeric
– String (text), lists, dictionaries, etc
– Basically anything you can’t add up using a
simple plus sign (+)
![Page 13: Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649db55503460f94aa6793/html5/thumbnails/13.jpg)
Not a String? Not a Problem!
• You can format outputting variables
you’ve already defined
x = 42
print “The value of x is”, x,
“.”
• What does this print out?
![Page 14: Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649db55503460f94aa6793/html5/thumbnails/14.jpg)
Not a String? Not a Problem!
• The output is
– The value of x is 42.
• The bottom will cause an error.
x = 42
print “$” + x
• We can’t combine string and numbers. So
what do we do?
![Page 15: Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649db55503460f94aa6793/html5/thumbnails/15.jpg)
Not a String? Not a Problem!
• We can make our numerical variable
a string!
x = 42
print “$” + str(x)
• This will print out $42
![Page 16: Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649db55503460f94aa6793/html5/thumbnails/16.jpg)
More on Variables• Variables can hold all kind of values, including strings,
numbers, and user input
• To assign a string value to a variable, you have to wrap the
string in quotes
firstName = “Jessi”
lastName = “Cheung”
mathProblem = “5 + 5”
print lastName, “,”, firstName, “;”, mathProblem
• What will this print?
![Page 17: Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649db55503460f94aa6793/html5/thumbnails/17.jpg)
More on Variables
• The output is: Cheung, Jessi; 5 + 5
• Variables can also be assigned new values that
are relative to their old values
total = 10
print “Original total:”, total
total = total + 4
print “New total:”, total
• What does this print?
![Page 18: Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649db55503460f94aa6793/html5/thumbnails/18.jpg)
More on Variables• The output is
Original total: 10
New total: 14
• Remember: a variable has to be defined on a previous line
before it can be used on the right-hand side of an equation
ABC = ABC + 4
print “ABC:”, ABC
• ERROR. There was no mention of the value of “ABC”
before the line trying to redefine it.
![Page 19: Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649db55503460f94aa6793/html5/thumbnails/19.jpg)
Python Arithmetic• Try typing the following code in your program area and see what comes out!
def main():
a = 12
b = 2
c = 16
d = 3
e = 2.5
print “The value of a is”, a
print (a / b) * 5
print a + b * d
print (a + b) * d
print b ** d
print c – e
a = a + b
print “The value of a is”, a
![Page 20: Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649db55503460f94aa6793/html5/thumbnails/20.jpg)
Python Arithmetic
• Is this what you got?
the value of a is 12
30
18
42
8
13.5
the value of a is 14
![Page 21: Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649db55503460f94aa6793/html5/thumbnails/21.jpg)
Exercise time!
• Write a program that takes in a
Celsius temperature (celsius) and
returns the temperature in
Fahrenheit
– Hint: To get Fahrenheit, multiply the
Celsius by (9.0/5.0) and add 32
![Page 22: Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649db55503460f94aa6793/html5/thumbnails/22.jpg)
Taking User Input
• Sometimes, instead of passing in an
argument as a parameter, we can
have the computer ask us what we
want!
![Page 23: Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649db55503460f94aa6793/html5/thumbnails/23.jpg)
Taking User Inputname = requestString("Enter your name:")
print name
first pops up a dialog box (where you can enter a name, say
‘John Doe’):
then outputs
John Doe
![Page 24: Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649db55503460f94aa6793/html5/thumbnails/24.jpg)
Taking User Input
• Let’s try it with numbers!
def requestNumber():
num = input(“Enter a number:”)
print “Your number is:”, num
print “Your number squared:”,
num*num
![Page 25: Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649db55503460f94aa6793/html5/thumbnails/25.jpg)
Taking User Input
• What if you tried inserting a string into…
def requestNumber():
num = input(“Enter a number:”)
print “Your number is:”, num
• If you type hello, there will be an error. If you
type “hello”, it will work
• This is where raw_input comes into play!
![Page 26: Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649db55503460f94aa6793/html5/thumbnails/26.jpg)
Taking User Input
• raw_input will take exactly what you
type and make it into a string
def requestName():
name = raw_input(“Enter your
name:”)
print name, “is awesome!”
• Try typing in a number!
![Page 27: Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649db55503460f94aa6793/html5/thumbnails/27.jpg)
Let’s Write a Program!
• Let’s write a program that will calculate the area and
the circumference of a circle!
• Open a new window (File/New Window)!
• At the top of your (blank) file, write the following:
# file name: circle.py
# author: Jessi Cheung
# description: a program to calculate the area and
the circumference of a circle
• Save the program as circle.py
![Page 28: Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649db55503460f94aa6793/html5/thumbnails/28.jpg)
Let’s Write a Program
• Let’s define this program as main.
def main():
• Your turn!
– Use input to ask the user for the
radius!
![Page 29: Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649db55503460f94aa6793/html5/thumbnails/29.jpg)
Let’s Write a Program
• We now have (besides our heading)
def main():
radius = input(“What is the radius? “)
• Now let’s start the calculations!
– Circumference of a circle: Pi (3.14) times (radius
times two)
– Area of a circle: Pi (3.14) times (radius squared)
![Page 30: Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649db55503460f94aa6793/html5/thumbnails/30.jpg)
Let’s Write a Program!
• We now have (besides our heading)
def main():
radius = input(“What is the radius?
“)
circumference = 3.14 * (2 * radius)
area = 3.14 * (radius * radius)
![Page 31: Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649db55503460f94aa6793/html5/thumbnails/31.jpg)
Let’s Write a Program!
• Now for the finishing touches!
• Let’s print out the output so we can
see it!
![Page 32: Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649db55503460f94aa6793/html5/thumbnails/32.jpg)
Let’s Write a Program
def main():
radius = input(“What is the radius? “)
circumference = 3.14 * (2 * radius)
area = 3.14 * (radius * radius)
print “The radius of our circle is”, radius
print “The circumference of our circle is”,
circumference
print “The area of our circle is”, area
![Page 33: Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649db55503460f94aa6793/html5/thumbnails/33.jpg)
Let’s RUN the Program!
• Once you save your program, press
F5 on your keyboard
• Nothing happens?!
![Page 34: Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649db55503460f94aa6793/html5/thumbnails/34.jpg)
Let’s RUN the Program!
• You must call your program!
• Call using main()
![Page 35: Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649db55503460f94aa6793/html5/thumbnails/35.jpg)
Let’s RUN the Program
• Another way you could run the
program…
![Page 36: Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649db55503460f94aa6793/html5/thumbnails/36.jpg)
For Loops
• Also known as the “definite loop” – we know
exactly how many times the loop will happen!
• Allows you to specify a list of items (numbers,
words, letters, etc.) and specify actions to be
performed on each one
• The official syntax for the for loop is:
for <var> in <sequence>:
<body>
![Page 37: Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649db55503460f94aa6793/html5/thumbnails/37.jpg)
Help the Kittens!
• You are working at an animal shelter, and you’re asked
to take a group of kittens and bathe, dry, and feed each
one individually
![Page 38: Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649db55503460f94aa6793/html5/thumbnails/38.jpg)
Use a Loop!
• Using a for-loop type notation, your
instructions would look like this:
Kittens = [kitty #1, kitty #2, kitty
#3, ...]
for kitty in Kittens:
bathe kitty
dry kitty
feed kitty
![Page 39: Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649db55503460f94aa6793/html5/thumbnails/39.jpg)
Basic Loop
• See what happens when you put in
this:
phrase = “Hello world!”
for letter in phrase:
print “the next letter is:”, letter
![Page 40: Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649db55503460f94aa6793/html5/thumbnails/40.jpg)
Basic Loop• The output!
the next letter is: H
the next letter is: e
the next letter is: l
the next letter is: l
the next letter is: o
the next letter is:
the next letter is: w
the next letter is: o
the next letter is: r
the next letter is: l
the next letter is: d
the next letter is: !
![Page 41: Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649db55503460f94aa6793/html5/thumbnails/41.jpg)
What Just Happened?
• Python went through the string one
character at a time, treating the
string like a sequence
• That means that the string can be
split into its components (the
characters)
![Page 42: Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649db55503460f94aa6793/html5/thumbnails/42.jpg)
Accumulator Variables
• When you’re using a for loop,
sometimes you might want to keep a
running total of numbers you’re
calculating, or recombine bits of a
string
![Page 43: Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649db55503460f94aa6793/html5/thumbnails/43.jpg)
Accumulator Variables
• Steps:
1. Define a variable for the first time before the loop starts
2. Redefine it as itself plus some operation in the body of
the for loop
total = 0
for num in [1,2,4,10,20]:
total = total + num
print “Total:”, total
• This will give the output
Total: 37
![Page 44: Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649db55503460f94aa6793/html5/thumbnails/44.jpg)
Accumulator Variables
• What is the point of accumulator
variables?
– Counting
– Keeping score
– Debugging
![Page 45: Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649db55503460f94aa6793/html5/thumbnails/45.jpg)
Conditional Statements
• Equals: ==
• Does not equal: !=
• Try this:
x = 1
if (x != 2):
print “Artemis rocks”
![Page 46: Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649db55503460f94aa6793/html5/thumbnails/46.jpg)
Want to learn more?
• Go to:
wiki.python.org/moin/BeginnersGuide