Welcome to Ms. Olifer's site



Python is an interpreted language. Python expressions and statements can be run in an interactive programming environment called the shell. Python file has an extension of .pyColors of the elements in Python:Black – inputs in the IDLE shell, numbers, operator symbols, variables, functions, method references, punctuation marks (ex. 57, +, y = factorial(x)Blue – outputs in the IDLE shell, class, and method names in definitions (ex. ‘Ken Lambert’, def factorial (n) )Green – strings (ex. “Ken Lambert”)Orange – keywords (ex. def, if, while)Purple – built-in function name (ex. abs, round, int)Red – program comments, error messages in the IDLE shell (ex. # Output results, ZeroDivisionError: division by zero)To add comments to your Python script, you can use the?#?tag. These comments are not run as Python code, so they will not influence your result.?Python is perfectly suited to do basic calculations. Apart from addition, subtraction, multiplication and division, there is also support for more advanced operations such as:Exponentiation:?**. This operator raises the number to its left to the power of the number to its right. For example?4**2?will give?16.Modulo:?%. This operator returns the remainder of the division of the number to the left by the number on its right. For example?18 % 7?equals?4.Ex. # Addition and subtractionprint(5 + 5)print(5 - 5)# Multiplication and divisionprint(3 * 5)print(10 / 2)# Exponentiationprint(4 ** 2)# Moduloprint(18 % 7)In Python, a variable allows you to refer to a value with a name. To create a variable use?=, like this example: x = 6. Remember,?=?in Python means?assignment, it doesn't test equality.Ex. # Create a variable savingssavings = 200# Create a variable factorfactor=1.10# Calculate resultresult=savings*factor**5# Print out resultprint(result)Other variable typesint, or integer: a number without a fractional part (ex. 120 is an integer).float, or floating point: a number that has both an integer and fractional part (ex.?1.10 is a float).Next to numerical data types, there are two other very common data types:str, or string: a type to represent text. You can use single or double quotes to build a string.bool, or boolean: a type to represent logical values. Can only be?True?or?False?(the capitalization is important!).To find out the type of a value or a variable that refers to that value, you can use the?type()?function. Suppose you've defined a variable?a, but you forgot the type of this variable. To determine the type of?a, simply execute: type(a)Type conversions:Using the?+?operator to paste together two strings can be very useful in building custom messages.You'll need? HYPERLINK "" \l "func-str" \t "_blank" str(), to convert a value into a string.?str(savings), for example, will convert the float?savings?to a string.Similar functions such as? HYPERLINK "" \l "int" \t "_blank" int(),?float()?and?bool()?will help you convert Python values into any type. ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download