Download presentation
Presentation is loading. Please wait.
Published byCorey Warren Modified over 9 years ago
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: 1. words and 2. 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 1. age = 15 2. min = age * 365 * 24 * 60 3. print("you are " + str(min) + " minutes old.") 4. if(age < 20): 5. print("You are still young!") 6. else: 7. print("You are getting old!")
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.