Programming (Python)

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

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

variable

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

string

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

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(), type

Finally, the ____ function returns the ____ 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 _

<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

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.

0, 0, 1

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

%

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!

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

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 ____.

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.

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.

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!

called

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

print

____ simply displays your code in the console.

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.

min()

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

!=

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 _____.

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 ____

+

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.

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.`

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.

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.

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.

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!'

functions, header, def, name, parameters

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

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.

if

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

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.

string methods

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

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.

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.

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?

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)

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.

=

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


Ensembles d'études connexes

Chapter 15: Employee assistance programs, private practice, coaching, and managed care

View Set

Unit 4a (Module 16-18) Study Guide

View Set

Formulating Scientific Questions

View Set

AP Computer Science Principles Midterm

View Set

1Z0-1001 Oracle Order Management Cloud 2018 Implementation Essentials

View Set