Presentation is loading. Please wait.

Presentation is loading. Please wait.

Python Arithmetic Operators OperatorOperationDescription +AdditionAdd values on either side of the operator -SubtractionSubtract right hand operand from.

Similar presentations


Presentation on theme: "Python Arithmetic Operators OperatorOperationDescription +AdditionAdd values on either side of the operator -SubtractionSubtract right hand operand from."— Presentation transcript:

1 Python Arithmetic Operators OperatorOperationDescription +AdditionAdd values on either side of the operator -SubtractionSubtract right hand operand from left hand operand *MultiplicationMultiplies values on either side of the operator /DivisionDivides left hand operand by right hand operand %ModulusDivides left hand operand by right hand operand and returns remainder **ExponentPerforms exponential (power) calculation on operands

2 Python Variables What’s Variable? Variables are like labels. It describes a place to store information. Python uses the equal sign (=) to assign values to variables. The operand to the left of the = operator is the name of the variable and the operand to the right of the = operator is the value stored in the variable. Python has five standard data types Numbers (integer, floating point number) String List Tuple Dictionary

3 Naming Convention ● It can be only one word ● It can use only letters, numbers, and the underscore (_) character ● It can't begin with a number Valid variable namesInvalid variable names maxAmountmax-Amount max_amountmax amount SPAM42 _spamtotal_$um, 'hello' account44account

4 Compound Data Types Strings – Enclosed in Quotes, holds characters, (immutable): “This is a String” Tuples – Values separated by commas, (usually enclosed by parenthesis) and can hold any data type (immutable): (4, True, “Test”, 34.8)‏ Lists – Enclosed in square brackets, separated by commas, holds any data type (mutable): [4, True, “Test”, 34.8] Dictionaries – Enclosed in curly brackets, elements separated by commas, key : value pairs separated by colons, keys can be any immutable data type, values can be any data type: { 1 : “I”, 2 : ”II”, 3 : “III”, 4 : “IV”, 5 : “V” }

5 String myString = “This is a test.” You can access a specific element using an integer index which counts from the front of the sequence (starting at ZERO!)‏ myString[0] produces 'T' myString[1] produces 'h' myString[2] produces 'i' myString[3] produces 's' The len() function can be used to find the length of a sequence. Remember, the last element is the length minus 1, because counting starts at zero! myString[ len(myString) – 1] produces '.'

6 List Lists are a mutable data type that you can create by enclosing a series of elements in square brackets separated by commas. The elements do not have to be of the same data type: myList = [ 23, True, 'Cheese”, 3.1459 ] Unlike Strings and tuples, individual elements in a list can be modified using the assignment operator. After the following commands: myList[0] = True myList[1] = 24 myList[3] = “Boo” myList contains: [ True, 24, 'Cheese', 'Boo' ]

7 Tuple A tuple is like a list that uses parenthese. Numbers=(0,1,2,3,4,5) The difference is you can’t change. Wrong: Numbers[0]=6

8 OperatorMeaning ==Equal to !=Not equal to <Less than >Greater than <=Less than or equal to >=Greater than or equal to Comparison Operators Logical Operators OperatorMeaning andTrue if both the operands are true orTrue if any of the two operands are non-zero notReverse the logical state of the operand

9 Exercises: >>>a=6 >>>(a>4) or (a <8) >>>4<a<8 >>>1<a<3 >>> a!=66 >>>a='hello' >>>a == 'hello' >>>not a>3 >>>not 0 >>>b='hello' >>>b=="hello"

10 If you have two mutually exclusive choices, and want to guarantee that only one of them is executed, you can use an IF/ELSE statement. The ELSE statement adds a second block of code that is executed if the boolean expression is false. if boolean_expression : STATEMENT else: STATEMENT Flow Control

11 IF/ELSE Example: numberOfWheels = 3 if ( numberOfWheels < 3): print(“You are a motorcycle!”) else: print(“You are a Car!”) print(“You have”, numberOfWheels, “wheels”) The last print statement is executed no matter what. If numberOfWheels is less than 3, it's called a motorcycle, otherwise it's called a car!

12 IF/ELIF/ELSE If you have several mutually exclusive choices, and want to guarantee that only one of them is executed, you can use an IF/ELIF/ELSE statements. The ELIF statement adds another boolean expression test and another block of code that is executed if the boolean expression is true. if boolean_expression : STATEMENT elif 2 nd_ boolean_expression ): STATEMENT else: STATEMENT

13 IF/ELIF/ELSE Example: numberOfWheels = 3 if ( numberOfWheels == 1): print(“You are a Unicycle!”) elif (numberOfWheels == 2): print(“You are a Motorcycle!”) elif (numberOfWheels == 3): print(“You are a Tricycle!”) elif (numberOfWheels == 4): print(“You are a Car!”) else: print(“That's a LOT of wheels!”) Only the print statement from the first true boolean expression is executed.

14 Input Function userName = input(“What is your name?”)‏ userAge = int( input(“How old are you?”)‏ ) birthYear = 2007 - userAge print(“Nice to meet you, “ + userName) print(“You were born in: “, birthYear) input() is guaranteed to give us a string, no matter WHAT the user enters. But what happens if the user enters “ten” for their age instead of 10?


Download ppt "Python Arithmetic Operators OperatorOperationDescription +AdditionAdd values on either side of the operator -SubtractionSubtract right hand operand from."

Similar presentations


Ads by Google