CMIS 102 arithmetic operators, int and float numbers, strings You need to be able to read and write arithmetic expressions. You also need to be able to take a math or science formula and turn it into a Python expression. Python can do all the basic arithmetic of grade school: addition, subtraction, multiplication, division, and inverse hyperbolic trigonometric functions (you missed that day?). The 7 arithmetic operators (** * / // % + -) have the same PEMDAS precedence relationships as they do in math (it would be weird if they didn't). [Python has about 40 operators; all are in some precedence group. We'll learn only some of them in this course.] Higher precedence means the operator is done before an operator of lower precedence. So, in this expression: x + 5 * z the multiplication is done before the addition because * has higher precedence than +. Terminology: + and * are "operators", the x, 5, and z above are "operands". Parentheses override the precedence of operators, just like in math. So, in: (x + 5) * z the + in the parens is done before the *. There's no "4a" meaning "4 times a" like in algebra. 4*a the multiplication operator must be written. In Python, if a number does not have a decimal point it is an integer. Division is of two kinds: / and // real number division: 7 / 3 equals 2.33333 integer division, ie. whole number division, 7 // 3 equals 2 Division by zero causes a crash. Python has an arithmetic operator not found in math: % which is the remainder operator also called "mod" (short for modulus) operator. Nothing to do with percent! It's a remainder operation, the remainder after integer division: 7 % 2 equals 1, the remainder after dividing 7 by 2 (think about when you first learned division, "2 goes into 7 three whole times, with a remainder of 1"). This % operator has nothing to do with percent; some punctuation symbol was needed, and % does have a / in it, sort of like division. % is in the same precedence level as *, /, and //. Mod by 0 isn't allowed. Evaluate: 10%9 10%8 10%7 10%6 10%5 10%4 10%3 10%2 if remainder is 0 after dividing by 2, is an even number 10%1 anything mod 1 is? 10%0 trick 10%11 and all other numbers >10? The mod operator is very useful: Ex. To get the number of whole minutes in a large number of seconds, integer divide the seconds by 60 and mod it by 60 to get the remainder seconds. 320 seconds is how many minutes and seconds? 320//60 minutes and 320%60 seconds. Ex. inches and feet, yards etc. 1000 inches is how many feet and remainder inches? 10,000 yards is how many miles, yards, feet, and inches? ** is the exponentiation operator: a**b means a to the bth power. x**2 is x squared. x**3 is x cubed. x**y is x to the y power Can be real numbers. ** is the only arithmetic operator that is right-associative, the others are left-associative (which only affects subtraction and divisions). x ** 2 ** 3 is same as x ** (2 ** 3) Rarely ever needed. Learn the powers of 2: 2**10 kilo 2**20 mega 2**30 giga 2**40 tera 2**50 peta 2**60 exa You need to be aware that ints and floats are stored ("represented") in the machine in different formats. The bits that make up int 2 are different from the bit pattern that makes float 2. But we can freely mix ints and floats in expressions and the interpreter will make everything the same (basically by automatically converting the int values to float format). This automatic conversion is called coercion. If in an expression there's at least one float, the result will be float. Only if all values are ints will the result be int (except /). x = 1234.56789 print(round(x), round(x,3)) String stuff: Strings can be "concatenated" (joined together one after the other) s1 = "hello" s2 = "world" s3 = s1 + s2 #because s1 and s2 are strings, the + is string concatenation operator print(s3) A string can be repeatedly concatenated with itself: s1 = "hello" s2 = s1 * 5 print(s2) #s1 repeated 5 times print('*' * 12) #(this 5 and 12 could be int variables or expressions that evaluate to an int) num_stars = int(input("Enter the length of asterisks you want: ")) print('*'*num_stars) Remove all spaces and tabs from beginning and end of string: str_var.strip() but does not change the str_var The var.method_name(maybe args...) is a method, a function that works on the value of the var variable. s = " hello world " print(s) print(s.strip()) print(s) # unchanged s = s.strip() print(s) Capitalize first character of a string: (if is non-letter, no effect) str_var.capitalize() Capitalize first character of each word of a string: str_var.title()