Python

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

How to type a comment?

#

Example of Functions

# Define a function my_function() with parameter x def my_function(x): return x + 1 # Invoke the function print(my_function(2))# Output: 3 print(my_function(3 + 5)) # Output: 9

Example of Integers

# Example integer numbers chairs = 4 tables = 1 broken_chairs = -2 sofas = 0 # Non-integer numbers lights = 2.5 left_overs = 0.0

Example of Floating point numbers

# Floating point numbers pi = 3.14159 meal_cost = 12.99 tip_percent = 0.20

Examples of function identification

# Indentation is used to identify code blocks def testfunction(number): # This code is part of testfunction print("Inside the testfunction") sum = 0 for x in range(number): # More indentation because 'for' has a code block # but still part of he function sum += x return sum print("This is not part of testfunction")

Modulo operator examples

# Modulo operations zero = 8 % 4 nonzero = 12 % 5

Examples of String concatenation

# String concatenation first = "Hello " second = "World" result = first + second long_result = first + second + "!"

Example of variable

# These are all valid variable names and assignment user_name = "@sonnynomnom" user_id = 100 verified = False # A variable's value can be changed after assignment points = 100 Points = 120

Python supports different types of arithmetic operations that can be performed on literal numbers, variables, or some combination. The primary arithmetic operators are: + for addition - for subtraction * for multiplication / for division % for modulus (returns the remainder) ** for exponentiation

Arithmetic Operations

Python uses simple syntax to use, invoke, or call a preexisting function. A function can be called by writing the name of it, followed by parentheses. For example, the code provided would call the doHomework() method.

Calling functions

# Comment on a single line user = "JDoe" # Comment after code ex. of ?

Comment

is a piece of text within a program that is not executed. It can be used to provide additional information to aid in understanding the code. The # character is used to start a comment and it continues until the end of the line.

Comment

variables can be assigned different types of data. One supported data type is the floating point number. A floating point number is a value that contains a decimal portion. It can be used to represent numbers that have fractional quantities. For example, a = 3/5 can not be represented as an integer, so the variable a is assigned a floating point value of 0.6. # Floating point numbers pi = 3.14159 meal_cost = 12.99 tip_percent = 0.20

Floating Point Numbers

Python uses indentation to identify blocks of code. Code within the same block should be indented at the same level. A Python function is one type of code block. All code under a function declaration should be indented to identify it as part of the function. There can be additional indentation within a function to handle other statements such as for and if so long as the lines are not indented less than the first line of the function code.

Function identification

Some tasks need to be performed multiple times within a program. Rather than rewrite the same code in multiple places, a function may be defined using the def keyword. Function definitions may include parameters, providing data input to the function. Functions may return a value using the return keyword followed by the value to return

Functions

is a number that can be written without a fractional part (no decimal). An integer can be a positive number, a negative number or the number 0 so long as there is no decimal portion. The number 0 represents an integer value but the same number written as 0.0 would represent a floating point number.

Integers

calculation returns the remainder of a division between the first and second number. For example: The result of the expression 4 % 2would result in the value 0, because 4 is evenly divisible by 2 leaving no remainder. The result of the expression 7 % 3would return 1, because 7 is not evenly divisible by 3, leaving a remainder of 1.

Modulo Operator %

How to divide?

Modulo operator % 4%2 = 0 Since 4 goes evenly to 2

is reported by the Python interpreter when it detects a variable that is unknown. This can occur when a variable is used before it has been assigned a value or if a variable name is spelled differently than the point at which it was defined. The Python interpreter will display the line of code where the NameError was detected and indicate which name it found that was not defined. misspelled_variable_name NameError: name 'misspelled_variable_name' is not defined

Name error

operator += provides a convenient way to add a value to an existing variable and assign the new value back to the same variable. In the case where the variable and the value are strings, this operator performs string concatenation instead of addition. The operation is performed in-place, meaning that any other variable which points to the variable being updated will also be updated.

Plus-Equals Operator +=

How to print?

Print()

If you would like to print something what steps should you do ?

Print() Have it within the quotes "example"

Python supports the joining (concatenation) of strings together using the + operator. The + operator is also used for mathematical addition operations. If the parameters passed to the + operator are strings, then concatenation will be performed. If the parameter passed to + have different types, then Python will report an error condition. Multiple variables or literal strings can be joined together using the + operator.

String concatenation

is a sequence of characters (letters, numbers, whitespace or punctuation) enclosed by quotation marks. It can be enclosed using either the double quotation mark " or the single quotation mark '. If a string has to be broken into multiple lines, the backslash character \ can be used to indicate that the string continues on the next line.

Strings

is reported by the Python interpreter when some portion of the code is incorrect. This can include misspelled keywords, missing or too many brackets or parenthesis, incorrect operators, missing or too many quotation marks, or other conditions.

SyntaxError

is used to store data that will be used by the program. This data can be a number, a string, a Boolean, a list or some other data type. Every variable has a name which can consist of letters, numbers, and the underscore character _. The equal sign = is used to assign a value to a variable. After the initial assignment is made, the value of a variable can be updated to new values as needed.

Variable

s reported by the Python interpreter when it detects a division operation is being performed and the denominator (bottom number) is 0. In mathematics, dividing a number by zero has no defined value, so Python treats this as an error condition and will report a ZeroDivisionError and display the line of code where the division occurred. This can also happen if a variable is used as the denominator and its value has been set to or changed to 0.

ZeroDivisionError

Example of SyntaxError

age = 7 + 5 = 4 File "<stdin>", line 1 SyntaxError: can't assign to operator

Examples of Plus-Equals Operator +=

counter = 0 counter += 10 #This is equivalent to counter = 0 counter = counter + 10 # The operator will also perform string concatenation message = "Part 1 of message " message += "Part 2 of message"

Examples of calling functions

doHomework()

In Phyton we assign variables by using the

equals sign (=)

Example of Name error

misspelled_variable_name NameError: name 'misspelled_variable_name' is not defined

Example of ZeroDivisionError

numerator = 100 denominator = 0 bad_results = numerator / denominator ZeroDivisionError: division by zero

example of print

print("Hello World!") print(100) pi = 3.14159 print(pi)

Arithmetic Operations examples

result = 10 + 30 result = 40 - 10 result = 50 * 5 result = 16 / 4 result = 25 % 2 result = 5 ** 3

Example of strings

user = "User Full Name" game = 'Monopoly' longer = "This string is broken up \ over multiple lines"


Set pelajaran terkait

Krueger, Explorations in Economics 1e, Module 47

View Set

Chapter 6 - Health Care Marketing

View Set

LCSW Exam: Therapist Development Center

View Set

GEO Plates, Plate Boundaries, and Driving Forces

View Set

Bio - Chapter 20 - genes within population (a)

View Set