1 / 28

Python Introduction

Python Introduction. Professor Frank J. Rinaldo Creation Core - office 401. Python. Textbook Python Programming for the Absolute Beginner (2 nd Edition) By Michael Dawson Premier Books 2007 ISBN: 1-59299-073-8. IDLE Integrated Development Environment. Interactive Mode Type in code

ezhno
Télécharger la présentation

Python Introduction

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Python Introduction Professor Frank J. Rinaldo Creation Core - office 401

  2. Python • Textbook • Python Programming for the Absolute Beginner (2nd Edition) • By Michael Dawson • Premier Books • 2007 • ISBN: 1-59299-073-8

  3. IDLE Integrated Development Environment • Interactive Mode • Type in code • Immediately executed • Useful for quick learning or debugging

  4. IDLE Integrated Development Environment • Script Mode • Acts like a ‘usual’ editor • Type in multiple lines of code • Then ‘save’ the program • Then ‘run’ program

  5. 1st Program # Game Over # Demonstrates the print command # Michael Dawson - 12/26/02 print "Game Over" raw_input("\n\nPress the enter key to exit.")

  6. 2nd Program # Game Over - Version 2 # Demonstrates the use of quotes in strings # Michael Dawson - 1/9/03 print "Program 'Game Over' 2.0" #NOTE using ‘/’ to continue a line AND triple quoted double quotes “”” below print \ """ _____ ___ ___ ___ _____ / ___| / | / |/ | | ___| | | / /| | / /| /| | | |__ | | _ / ___ | / / |__/ | | | __| | |_| | / / | | / / | | | |___ \_____/ /_/ |_| /_/ |_| |_____| _____ _ _ _____ _____ / _ \ | | / / | ___| | _ \ | | | | | | / / | |__ | |_| | | | | | | | / / | __| | _ / | |_| | | |/ / | |___ | | \ \ \_____/ |___/ |_____| |_| \_\ """ raw_input("\n\nPress the enter key to exit.")

  7. Using Quotes • You can use 2 single quotes (‘) • print ‘Hello’ • You can use 2 double quotes (“) • print “Hello” • Either one is fine • Quotes in quotes • print “Bill said to say ‘Hi!’” prints: Bill said to say ‘Hi!’

  8. Newline • Just like in C/C++ the ‘\n’ goes to a new line • This allows you to space you output into a more readable form

  9. Escape Sequences • The newline (\n) is called an escape sequence • The backslash character (\) indicates something special • It changes the meaning of the following character! • For example: • ‘n’ is just the letter (lowercase) n • ‘\n’ is a newline!

  10. Other Escape Sequences • \\ Backslash (the first backslash indicates that the second backslash in just the normal backslash character!) • \’ Is just the character ‘ • NOT the beginning or end of a string • \” Is just the character “ • NOT the beginning or end of a string • \n is a newline (we saw this already!)

  11. More Escape Sequences • \a Rings a bell • \b Is a backspace • \t Is a tab

  12. Strings • A ‘string’ is just a sequence of characters • print ‘This is a string’ • Combine two strings by using the concatenate (+) operator • print ‘This is ‘ + ‘two strings combined’ • Repeating string • print “Pie” * 5 • PiePiePiePiePie • print ‘*’ * 80 • **********************************…**

  13. 3rd Program # Personal Greeter # Demonstrates getting user input # Michael Dawson 1/13/03 name = raw_input("Hi. What's your name? ") print name print "Hi, " + name raw_input("\n\nPress the enter key to exit.")

  14. Numbers • There are basically two kinds of number in Python • Integer • Floating point (has a decimal point in it) • Examples: • print 24 / 6 (answer 4 – integer arithmetic) • print 19 / 4 (answer 4 – integer arithmetic) • print 19.0 / 4 (answer 4.75 – floating point arithmetic)

  15. Arithmetic • Integer arithmetic ALWAYS uses whole numbers (integers) • Like: …-1, 0, 1, 2, 3, … • Floating point arithmetic ALWAYS uses decimal numbers (real numbers) • Like: 3.1415, 97.32, … • If you combine integers and floating point numbers in the same line integers will be (automatically) converted to floating point numbers

  16. Variables • A ‘variable’ is a way to store & reference some data • name = ‘frank’ • age = 21 • pi = 3.14159265358 • Unlike C/C++ & Java you do not have to ‘declare’ variables in Python • Simply use (reference) them

  17. Input • To get input into your program we use the ‘raw_input’ command • name = raw_input(“Enter your name: “) • This will print out the text “Enter your name: “ and then allow your to enter your name • The program will wait as you type until you type a newline (carriage return) (Enter key)

  18. String Methods • A ‘method’ is just object oriented name for some code (maybe with some data) • Example: • text = “This is some EXAMPLE text” • print text This is some EXAMPLE text • print text.upper() THIS IS SOME EXAMPLE TEXT • print text.lower() this is some example text

  19. Some String Methods • upper () – returns upper case • lower() – returns lower case • swapcase() – changes upper to lower & lower to upper • capitalize() – first letter is upper & rest lower • title() – first character of each word is upper & rest is lower • strip() – remove all beginning & ending whites spaces (blanks, tabs, newlines) • replace(old,new,max) – replace ‘old’ with ‘new’. Optonal ‘max’ is the maximum times to do it

  20. ConvertingStrings to integers • The input method ‘raw_input’ will input a string! How do you get a number!? • Example: • age = raw_input(“Enter your age: “) Note: age is a string! • age = int(age) Note: converts the string ‘age’ into an integer ‘age’

  21. Conversions • float(x) – converts ‘x’ into a floating point number • int(x) – converts ‘x’ into an integer • str(x) – converts ‘x’ into a string

  22. Assignment shortcuts • *= • x *= 5 (x = x * 5) • /= • x /= 5 (x = x / 5) • %= • x %= 5 (x = x % 5) • += • x += 5 (x = x + 5) • -= • x -= 5 (x = x – 5)

  23. 4th Program # Useless Trivia # # Gets personal information from the user and then # prints true, but useless information about him or her # # Michael Dawson - 1/14/03 name = raw_input("Hi. What's your name? ") age = raw_input("And how old are you? ") age = int(age) weight = raw_input("Okay, last question. How many pounds do you weigh? ") weight = int(weight) print "\nIf poet ee cummings were to email you, he'd address you as", name.lower() ee_mad = name.upper() print "But if ee were mad, he'd call you", ee_mad

  24. 4TH Program (cont…) dog_years = age / 7 print "\nDid you know that you're just", dog_years, "in dog years?" seconds = age * 365 * 24 * 60 * 60 print "But you're also over", seconds, "seconds old." called = name * 5 print "\nIf a small child were trying to get your attention, " \ "your name would become:" print called moon_weight = weight / 6.0 print "\nDid you know that on the moon you would weigh only", moon_weight, "pounds?" sun_weight = weight * 27.1 print "But on the sun, you'd weigh", sun_weight, "(but, ah... not for long)." raw_input("\n\nPress the enter key to exit.")

  25. 5th Program # Guess My Number # # The computer picks a random number between 1 and 100 # The player tries to guess it and the computer lets # the player know if the guess is too high, too low # or right on the money # # Michael Dawson - 1/8/03 import random print "\tWelcome to 'Guess My Number'!" print "\nI'm thinking of a number between 1 and 100." print "Try to guess it in as few attempts as possible.\n" # set the initial values the_number = random.randrange(100) + 1 guess = int(raw_input("Take a guess: ")) tries = 1

  26. 5th Program (Cont…) # guessing loop while (guess != the_number): if (guess > the_number): print "Lower..." else: print "Higher..." guess = int(raw_input("Take a guess: ")) tries += 1 print "You guessed it! The number was", the_number print "And it only took you", tries, "tries!\n" raw_input("\n\nPress the enter key to exit.")

  27. Class Program • Write a Python program in class • Have the computer guess MY number • Develop program specification • Develop pseudocode for program • Write, test AND demonstrate Python program

  28. Homework • Due Week 3 in Class • Write a Python Program to: • Accept a text message from the user and then prints it out backwards • Include: • Simple Specification Document • Simple Pseudocode • Python code • Demonstrate program in class

More Related