1 / 13

Introduction to Python

Introduction to Python. 1. The 5 operators in Python are:. + - * / %. 2. Setting variables. To create a variable you type the name and set it equal to an initial value Words go in quotes Example: Set name to Lia n ame = “ Lia ” Example: set score to 95 s core = 95 On your own:

arden-foley
Download Presentation

Introduction to Python

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. Introduction to Python

  2. 1. The 5 operators in Python are: • + • - • * • / • %

  3. 2. Setting variables • To create a variable you type the name and set it equal to an initial value • Words go in quotes • Example: Set name to Lia • name = “Lia” • Example: set score to 95 • score = 95 • On your own: • Set hoursWorked to 40

  4. 3. Calculations • Put the equation on the RIGHT side • Example: To calculate cost that equals price plus 0.95 you type: • cost = price + 0.95 • On your own: calculate totalPay that equals hoursWorked times 9.75

  5. 4. The rules for naming variables are: • Start with a letter(usually people use lower case letters) • There can't be any blank spaces or symbols ( like & % * # @ ) • Copy the ones that are GOOD variable names for Python: • sum • best/cost • 2Go • test1 • sales% • firstName • Last name Case matters! Sum is not the same as sum

  6. 5. Multi-step calculations • In Python, if you do calculations like 3+4*2, the computer will do the multiplication and division first and then the addition and subtraction • The answer to 3+4*2 is • 3+(4*2) = 3 + 8 = • What is 4 + 12 / 2 – 1?

  7. 6. Decimal points • In Python you only get a decimal point in your answer if there is one in your calculation, so • 7 / 2 = _____ • 26 / 5 = _____ • 10.0 / 4 = _____ • 3 • 5 • 2.5

  8. 7. Kinds of information • For the work we are doing, there are 2 different kinds of variables: • words and • numbers

  9. 8. Changing from numbers to words • When you want to “say” or “print” something in a sentence, it needs to be a string. • To change a number into a string use the str function. • For example • x = 5 • str(x)

  10. 9. Output • To output in Python you print • If you have 2 variables: x=6 and y=14 you can calculate their sum and print the equation with this code:The printout should show: The answer to 6 + 14 is 22 sum=_____________________ print(____________________________________________________________)

  11. 11. Equals • The way to ask if 2 things are equal is ==. • A = 5 sets a equal to 5 • A == 5 asks if a is equal to 5

  12. 12. if • To use an if/else statement you put the condition in parentheses and put a colon after the if and else lines.You must indent each statement after the if and else Example: if ( x==5): print (“x is 5”) else: print(“x is not 5”) • On your own: • Write the statement that says if guess equals answer print "right" otherwise print "wrong"

  13. Make a program Calculate your age in minutes • age = 15 • min = age * 365 * 24 * 60 • print("you are " + str(min) + " minutes old.") • if(age < 20): • print("You are still young!") • else: • print("You are getting old!")

More Related