Summary

These notes cover fundamental concepts in Python programming, including strings, escape characters, variables, data types, and mathematical operators. They provide examples of type casting and explain the rules and best practices for variable naming, offering a comprehensive guide to basic Python concepts.

Full Transcript

**Strings** Main escape characters used in Python: Code Result \\\" Double Quotes \\\' Single Quote \\\\ Backslash \\n New Line \\t Tab \\b Backspace print(\"Our logo \"GO BEARS GO\" \") -- error print(\"Our logo \\\"GO BEARS GO\\\" \") Output: Our logo \"GO BEARS GO\" **Type Casting**...

**Strings** Main escape characters used in Python: Code Result \\\" Double Quotes \\\' Single Quote \\\\ Backslash \\n New Line \\t Tab \\b Backspace print(\"Our logo \"GO BEARS GO\" \") -- error print(\"Our logo \\\"GO BEARS GO\\\" \") Output: Our logo \"GO BEARS GO\" **Type Casting** **int()** will return the value between the parentheses as an integer (rounding down if necessary) **float()** will return the value between the parentheses as a float (adding.0 if necessary) **str()** will return the value between the parentheses as a string For example: **int(3/2) -\> 1** **float(3) -\> 3.0** **str(5) -\> \"5\"** **int(\"7\") -\> 7** **int(\"abc\")-\> error** **print(\"sum is\" + 15) --\> error** **print(\"sum is\" + str(15)) --\> no errors** **print(\"3\"\*5) --\> \"33333\"** **print( int(\"3\") \*5) --\> 15** **Using strings** **Variables:** Variables are named locations in memory for the storage of data of a specified type. What variable types do we know about so far? Int (Integers) Str (String) We will add float to this list. A float is a real number. In Python, the compiler will declare a variable "on the fly" This is bad form! I expect all variables to be "declared" before they are used at the top of the program. **Rules for variables:** A variable name cannot contain spaces The first character cannot be any digit. It must be one of the letters a through z, A through Z, or an underscore \_. The rest can be the combination of letters a through z or A through *Z,* the digits 0 through 9 Can not use any special symbols inside a variable name except for \_ Uppercase and lowercase characters are distinct You can't use words reserved for Python. For example you can't use the word 'print' as a variable. It is a keyword When naming a variable, give it a name that is descriptive of its role **Variable Types:** Integer -- int, stores integer numbers String -- str, stores letters and words Floating point -- float, stores decimals Boolean -- True or False **What var type it should be:** Your age Your name Pi The distance between Toronto and Montreal Today's Date The number of apples in a bushel **Math Operators:** Cannot divide by zero Math sqrt- returns the square root of a number math.pi The mathematical constant π = 3.141592\..., to available precision. x=2\* math.pi\*15 \>\>\>math.pi 3.141592653589793 math.ceil(x) Return the ceiling of x, the smallest integer greater than or equal to x math.ceil(3.5) 4 math.ceil(-5.3) -5 🡪🡪 math.fabs(x) Return the absolute value of x. math.factorial(x) Return x factorial. Raises ValueError if x is not integral or is negative. math. factorial(3) 6 math. factorial(4) 24 🡪🡪 math.floor(x) Return the floor of x, the largest integer less than or equal to x. math. floor (3.5) 3 math. floor (-5.3) -6 🡪🡪 math.pow(x, y) Return x raised to the power y. math. pow (2,3) 8 🡪 math.sqrt(x) Return the square root of x. math.cos(x) Return the cosine of x radians. math.hypot(x, y) Return the Euclidean norm, sqrt(x\*x + y\*y). This is the length of the vector from the origin to point (x, y). math.sin(x) Return the sine of x radians. math.tan(x) Return the tangent of x radians. math.degrees(x) Converts angle x from radians to degrees. math.radians(x) Converts angle x from degrees to radians.

Use Quizgecko on...
Browser
Browser