Python

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

continue

Unlike break, continue jumps back to the top of the loop, rather than stopping it.

while loop

A control flow statement that allows code to be executed repeatedly.

modulo operator

Represented by the % symbol - returns the remainder of dividing two numbers.

Boolean type

A comparison operator with two possible values: true and false. (==)

Boolean Operators

AND, OR, and NOT

else statement

An else statement follows an if statement, and contains code that is called when the if statement evaluates to False. As with if statements, the code inside the block should be indented.

floor division

An operator (denoted by the token //) that determines the quotient - how many times one integer divides into another. >>>20//6=3

Concatentation

As with integers and floats, strings in Python can be added, using a process called concatenation with the "+" token, which can be done on any two strings. When concatenating strings, it doesn't matter whether they've been created with single or double quotes.

floats

Floats are used in Python to represent numbers that aren't integers. Some examples of numbers that are represented as floats are 0.5 and -7.8237591. They can be created directly by entering a number with a decimal point, or by using operations such as division on integers. Extra zeros at the number's end are ignored.

int function

In Python, it's impossible to complete certain operations due to the types involved. For instance, you can't add two strings containing the numbers 2 and 3 together to produce the integer 5, as the operation will be performed on strings, making the result '23'. The solution to this is type conversion. In that example, you would use the int function. Another example of type conversion is turning user input (which is a string) to numbers (integers or floats), to allow for the performance of calculations.

in place operators

In-place operators allow you to write code like 'x = x + 3' more concisely, as 'x += 3'. The same thing is possible with other operators such as -, *, / and % as well. These operators can be used on types other than numbers, as well, such as strings.

Lists

Lists are another type of object in Python. They are used to store an indexed list of items. A list is created using square brackets with commas separating items. The certain item in the list can be accessed by using its index in square brackets. Typically, a list will contain items of a single item type, but it is also possible to include several different types.

Auto New Line

Python provides an easy way to avoid manually writing "\n" to escape newlines in a string. Create a string with three sets of quotes ("""hello world""")and newlines that are created by pressing Enter and are automatically escaped for you.

String multiplication

Strings can also be multiplied by integers. This produces a repeated version of the original string. The order of the string and the integer doesn't matter, but the string usually comes first. Strings can't be multiplied by other strings. Strings also can't be multiplied by floats, even if the floats are whole numbers.

else if statement

The elif (short for else if) statement is a shortcut to use when chaining if and else statements. A series of if elif statements can have a final else block, which is called if none of the if or elif expressions is True.

Input

The function prompts the user for input, and returns what they enter as a string (with the contents automatically escaped).

index function

The index method finds the first occurrence of a list item and returns its index. If the item isn't in the list, it raises a ValueError. letters = ['p', 'q', 'r', 's', 'p', 'u'] print(letters.index('r')) print(letters.index('p')) print(letters.index('z'))

insert function

The insert method is similar to append, except that it allows you to insert a new item at any position in the list, as opposed to just at the end. words = ["Python", "fun"] index = 1 words.insert(index, "is") print(words)

not equal operator

The not equal operator (!=), evaluates to True if the items being compared aren't equal, and False if they are.

range function

The range function creates a sequential list of numbers. numbers = list(range(10)) print(numbers) >>> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>>

append

This adds an item to the end of an existing list. nums = [1, 2, 3] nums.append(4) print(nums)

exponentiation

This operation is performed using two asterisks.

in operator

To check if an item is in a list, the in operator can be used. It returns True if the item occurs one or more times in the list, and False if it doesn't.

not operator

To check if an item is not in a list, you can use the not operator in one of the following ways: nums = [1, 2, 3] print(not 4 in nums) print(4 not in nums) print(not 3 in nums) print(3 not in nums)

break

To end a while loop prematurely, the break statement can be used. When encountered inside a loop, the break statement causes the loop to finish immediately.

Assign a variable

Variables play a very important role in most programming languages, and Python is no exception. A variable allows you to store a value by assigning it to a name, which can be used to refer to the value later in the program. To assign a variable, use one equals sign. Unlike most lines of code we've looked at so far, it doesn't produce any output at the Python console. Certain restrictions apply in regard to the characters that may be used in Python variable names. The only characters that are allowed are letters, numbers, and underscores. Also, they can't start with numbers.

if statement

You can use if statements to run code if a certain condition holds. If an expression evaluates to True, some statements are carried out. Otherwise, they aren't carried out. If statements must be indented and have a colon at the end of the expression (first line).

del statement

You can use the del statement to remove a variable, which means the reference from the name to the value is deleted, and trying to use the variable causes an error. Deleted variables can be reassigned to later as normal.

New Line command

\n

interpreter

a program that runs scripts written in an interpreted language such as Python

Using a single slash to divide numbers does what?

produces a decimal (or float, as it is called in programming)

len function

returns the length of a sequence such as a list nums = [1, 3, 5, 2, 4] print(len(nums))

string

str - Basic type that stores text - created by entering text between two single or double quotation marks. Some characters can't be directly included in a string. For instance, double quotes can't be directly included in a double quote string; this would cause it to end prematurely. Characters like these must be escaped by placing a backslash before them. Other common characters that must be escaped are newlines and backslashes. Double quotes only need to be escaped in double quote strings, and the same is true for single quote strings.

IDLE

the integrated development environment, which includes tools for writing and debugging entire programs


Kaugnay na mga set ng pag-aaral

Chapter 15 Assessing Head / Neck Prep U

View Set

It's Your Paycheck Lesson 6 Test

View Set

Lippincott and Saunders Psych questions

View Set

Salesforce Certified Business analyst exam

View Set