460 likes | 737 Views
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 will execute. What is Python?. Python is a high level language that we (humans) can understand Other examples include C++ and Java
E N D
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
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!
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
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 = 1or msg = “Hello world!” • Variables can be changed throughout a program • For example: x = 1, x = x + 1
Python Vocab • Comments – notes ignored by the computer • For example: x + y # variables store user input • Operators – mathematical symbols • +, -, *, /, ** (exponents), == (equality)
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
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!
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!
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
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 (+)
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?
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?
Not a String? Not a Problem! • We can make our numerical variable a string! x = 42 print “$” + str(x) • This will print out $42
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?
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?
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.
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
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
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
Taking User Input • Sometimes, instead of passing in an argument as a parameter, we can have the computer ask us what we want!
Taking User Input name = requestString("Enter your name:") print name first pops up a dialog box (where you can enter a name, say ‘John Doe’): then outputsJohn Doe
Taking User Input • Let’s try it with numbers! defrequestNumber(): num = input(“Enter a number:”) print “Your number is:”, num print “Your number squared:”, num*num
Taking User Input • What if you tried inserting a string into… defrequestNumber(): 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!
Taking User Input • raw_input will take exactly what you type and make it into a string defrequestName(): name = raw_input(“Enter your name:”) print name, “is awesome!” • Try typing in a number!
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
Let’s Write a Program • Let’s define this program as main. def main(): • Your turn! • Use inputto ask the user for the radius!
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)
Let’s Write a Program! • We now have (besides our heading) defmain(): radius = input(“What is the radius? “) circumference = 3.14 * (2 * radius) area = 3.14 * (radius * radius)
Let’s Write a Program! • Now for the finishing touches! • Let’s print out the output so we can see it!
Let’s Write a Program defmain(): 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
Let’s RUN the Program! • Once you save your program, press F5 on your keyboard • Nothing happens?!
Let’s RUN the Program! • You must call your program! • Call using main()
Let’s RUN the Program • Another way you could run the program…
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>
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
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
Basic Loop • See what happens when you put in this: phrase = “Hello world!” for letter in phrase: print “the next letter is:”, letter
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: !
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)
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
Accumulator Variables • Steps: • Define a variable for the first time before the loop starts • 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
Accumulator Variables • What is the point of accumulator variables? • Counting • Keeping score • Debugging
Conditional Statements • Equals: == • Does not equal: != • Try this: x = 1 if (x != 2): print “Artemis rocks”
Want to learn more? • Go to: wiki.python.org/moin/BeginnersGuide