140 likes | 245 Views
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:
E N D
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 • name = “Lia” • Example: set score to 95 • score = 95 • On your own: • Set hoursWorked to 40
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
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
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?
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
7. Kinds of information • For the work we are doing, there are 2 different kinds of variables: • words and • numbers
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)
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. 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. 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"
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!")