Programming (Python)

Ace your homework & exams now with Quizwiz!

<type 'int'> <type 'float'> <type 'str'>

If you ask Python to do the following: print type(42) print type(4.2) print type('spam') Python will output

list

In Python, a comma-separated sequence of data items that are enclosed in a set of brackets is called a _____.

len()

Let's start with ____, which gets the length (the number of characters) of a string!

colons

Make sure the if and elif statements end with ____

int

A Python data type that holds positive and negative whole numbers.

variable

A _____ stores a piece of data, and gives it a specific name. For example: spam = 5

+=

Add AND. Adds right operand to the left and assigns the result to the left. A += B ~ A = A + B

called

After defining a function, it must be ____ to be implemented. In the previous exercise,

colons (if some_function(): returns True, then the indented block of code after it will be executed. In the event that it returns False, then the indented block will be skipped.)

Also, make sure you notice the ____ at the end of the if statement. We've added them for you, but they're important.

string

Another useful data type is the ____. A ____ can contain letters, numbers, and symbols.

can lists have more than one data type

Any structure can contain data of any type, but it makes most sense for the type of data to remain consistent (homogenous) fora given variable

not, and, or

Boolean operators aren't just evaluated from left to right. Just like with arithmetic operators, there's an order of operations for boolean operators: __ is evaluated first; ___ is evaluated next; __ is evaluated last.

string

Each character in a ____ is assigned a number. This number is called the index.

%

The _ operator after a string is used to combine a string with variables. The % operator will replace a %s in the string with the string variable that comes after it.

# (both)

The __ sign is for comments. A comment is a line of text that Python won't try to run as code. It's just for humans to read.

triple quotation

The __ sign will only comment out a single line. While you could write a multi-line comment, starting each line with __, that can be a pain. Instead, for multi-line comments, you can include the whole block in a set of ____ _____ marks: """Sipping from your cup 'til it runneth over, Holy Grail. """

boolean operator

The ___ ____ not returns True for false statements and False for true statements.

boolean operator

The ___ ____ or returns True when at least one expression on either side of or is true.

max()

The ___ function takes any number of arguments and returns the largest one.

str()

The ___ method converts non-strings into strings.

str()

The ___ method turns non-strings into strings!

+

The ___ operator between strings will 'add' them together, one after the other. Notice that there are spaces inside the quotation marks after Life and of so that we can make the combined string look like 3 words.

else, if, if/else

The ___ statement complements the __ statement. An __/___ pair says: "If this expression is true, run this indented code block; otherwise, run this code after the else statement."

abs(), max(), min()

The ____ function always returns a positive value, and unlike ____ and ____ it only takes a single number.

or

The boolean operator __ returns True when at least one expression on either side of or is true.

not

The boolean operator ___ returns True for false statements and False for true statements.

interpreter (both)

The window in the top right corner of the page is called the _____. The _____ runs your code line by line, and checks for any errors.

if

__ is a conditional statement that executes some specified code after checking if its expression is True.

string methods

___ ____ let you perform specific tasks for strings. We'll focus on four string methods: len() lower() upper() str()

modulo

____ returns the remainder from a division. So, if you type 3 % 2, it will return 1, because 2 goes into 3 evenly once, with 1 left over.

print

____ simply displays your code in the console.

min()

____ then returns the smallest of a given series of arguments.

/

decimal divisionw

header, name, parameters

def hello_world(). Here the def is the ____, the hello_world is the ___, and there are no ___.

Function

def shout(phrase): if phrase == phrase.upper(): return "YOU'RE SHOUTING!" else: return "Can you speak up?" shout("I'M INTERESTED IN SHOUTING") This is an example of how a ___ is structured.

parameter

def square(n): n is a _____ of square. A ____ acts as a variable name for a passed in argument (argument is the thign that will replace the parameter when you run the function)

In Python, you would use the ?? statement to write a count-controlled loop.

for

if, and, not

if age > 12 and age < 20: print("Teenager!") if team == "Yankees" or team == "Mets": print("New York baseball fan!" if not (year == "first year"): print("Not a frosh!")

true

if is a conditional statement that executes some specified code after checking if its expression is __.

floats

in python, _____ are numbers with decimals.

**

in python, we use __ to indicate exponents.

*

in python, we use __ to indicate multiplication

//

integer division

console

the ___ is where the results of the code are shown

print type(42) print type(4.2) print type('spam')

the answers: <type 'int'> <type 'float'> <type 'str'> come from what?

editor

the area where you write your code

quotes

the thing that separates strings from variables is that string needs to be in ____.

Try-Except Statement

try: statement(s) except ExceptionName: statement(s)

float

A Python data type which stores floating-point numbers. Floating-point numbers are stored internally in two parts: a base and an exponent. When printed in the standard format, they look like decimal numbers. Beware of rounding errors when you use floats, and remember that they are only approximate values.

module

A ____ is a file that contains definitions—including variables and functions—that you can use once it is imported.

boolean

A _____ is like a light switch. It can only have two values. Just like a light switch can only be on or off, a _____ can only be True or False

upper()

Call _____ on parrot (after print on line 3) in order to capitalize all the characters in the string!

concatenation

Combining strings together like this is called _____. Let's try concatenating a few strings together now! "22"+"22"="2222"

number

Each character in a string is assigned a ____. This number is called the index.

index

Each character in a string is assigned a number. This number is called the _____.

==

Equal to (___)

type()

Finally, the ____ function returns the type of the data it receives as an argument.

max

For example, ___(1,2,3) will return 3 (the largest number in the set of arguments)

3

For example, max(1,2,3) will return _

0, 1

In Python, we start counting the index from ___ instead of __

=, ==

In the above Python code, make sure to note the difference between the equals symbol (___), in which we assign a value to a variable, and the double equals symbol (___), in which we test the equality of two variables.

!=

Not equal to (___)

.upper(), .lower(), str(), len()

Now that you understand what functions are and how to import modules, let's look at some of the functions that are built in to Python (no modules required!). You already know about some of the built-in functions we've used with strings, such as _____, _____, ____, and _____.

abstraction

Programming languages use functions as a means of abstraction. The program can define a function in one single place, and then call that function over and over again Avoids repeated code Think about driving. How many of you know how a car works? How many can drive a car? Abstraction!

0, 0, 1

Python uses _-based indexing, in which the first symbol of the string occurs at position _ instead of _

abs()

The ____ function returns the absolute value of the number it takes as an argument—that is, that number's distance from 0 on an imagined number line. For instance, 3 and -3 both have the same absolute value.`

and

The boolean operator ___ returns True when the expressions on both sides of and are true. For instance: 1 < 2 and 2 < 3 is True; 1 < 2 and 2 > 3 is False.

def

The keyword ___ at the beginning of a function signals that we are "defining" a function.

backslash

There are some characters that cause problems. For example: 'There's a snake in my boot!' This code breaks because Python thinks the apostrophe in 'There's' ends the string. We can use the _____ to fix the problem, like this: 'There\'s a snake in my boot!'

eval

This function is needed to collect and store numeric inputs.

function

You might have considered the situation where you would like to reuse a piece of code, just with a few different values. Instead of rewriting the whole code, it's much cleaner to define a _____, which can then be used repeatedly.

comment

You probably saw us use the # sign a few times in earlier exercises. The # sign is for comments. A ____ is a line of text that Python won't try to run as code. It's just for humans to read.

Boolean operators

___ ___ compare statements and result in boolean values.

control flow

___ ____ gives us this ability to choose among outcomes based off what else is happening in the program.

elif

___ is short for "else if." It means exactly what it sounds like: "otherwise, if the following expression is true, do this!"

lower()

____ is another function that you can do to strings and it gets rid of all the capitalization in your strings.

functions, header, def, name, parameters

_____ are defined with three components: The ____, which includes the ___ keyword, the ___ of the function, and any ____ the function requires.

functions, header, def, name, parameters, comment, body

_____ are defined with three components: The ____, which includes the ___ keyword, the ___ of the function, and any ____ the function requires. The second is an optional ___ which explains what the fucntion does. The last is the ___, which describe the procedure the function carries out.

function

_____ are defined with three components: The header, which includes the def keyword, the name of the function, and any parameters the function requires.

function

a ____ takes an input and returns an output.

input, output

a function takes an ___ and returns an ___

%

a modulo is represented by what between numbers?

and, or, not

there are three boolean operators: ___, which checks if both statements are true, ___ which checks if at least one of the statements is true, and ___, which gives the opposite of the statement.

header, comment, body

what are the three main parts of a function? Which one is indented?

index

what is this an example of: c = "cats"[0] n = "Ryan"[3]

4

when dealing with whitespaces, indent the code with the use of __ spaces.

What is the format for the while clause in Python

while condition: statement

=

you can assign a variable to a value with the operator __.


Related study sets

Slope & Parallel and Perpendicular Lines

View Set

Chapter 2: Select the Correct Answer Exercise

View Set

SIE Practice Mastery Exam (dont get it twisted)

View Set

Chapter 1 Study Guide Quiz Questions

View Set

PN2 NCLEX Style Questions Exam 3

View Set

Two Independent Samples Checkpoint

View Set

Chapter 10 Social Class in the United States

View Set