Python Statements

¡Supera tus tareas y exámenes ahora con Quizwiz!

Explain the concept of lambda expressions

lambda expressions are black box functions that take inputs and return a result. We don't necessarily care about what they return, but what they do (think limits). Lambda is a tool for building functions, or more precisely, for building function objects. That means that Python has two tools for building functions: def and lambda.

Create a list of 0 to 10 using the range function

list(range(0,11))

Use range() to print all the even numbers from 0 to 10.

list(range(0,11,2)

Python 3 range( ) function

range( ) is a generator and you don't need to worry about using xrange( )

Explain the range( ) function:

range() allows us to create a list of numbers ranging from a starting point up to an ending point. We can also specify step size. where range(start, end, step)

Create a Lambda expression that is equivalent to the function def square(num): result = num**2 return result

square = lambda num : num**2 square(4) output: 16 The lambda expression is a black box. You put in x and get x**2. There is a not a defined internal body of code that is why lambda functions are anonymous.

Write code that squares each number in the range (0,11). Make sure the output is in the form of a list.

square_list = [x**2 for x in range(0,11)] square_list

Break, Continue, and Pass

We can use break, continue, and pass statements in our loops to add additional functionality for various cases. The three statements are defined by: break: Breaks out of the current closest enclosing loop. continue: Goes to the top of the closest enclosing loop. pass: Does nothing at all.

Use List Comprehension to create a list of the first letters of every word in the string below: st = 'Create a list of the first letters of every word in this string'

[word[0] for word in st.split()] ['C', 'a', 'l', 'o', 't', 'f', 'l', 'o', 'e', 'w', 'i', 't', 's']

Use List comprehension to create a list of all numbers between 1 and 50 that are divisible by 3.

[x for x in range(1,50) if x%3 == 0]

Generator

d.items( )

Illustrate the general format for a WHILE Loop

while test: code statement else: final code statements

Contrast Methods and Attributes

A Attribute is any object value: object.attribute While a Method is callable attribute defined in a class or built-in (essentially a function) Every Method is an attribute, but not every attribute is a method

Explain what FOR Loop is

A for loop acts as an iterator in Python, it goes through items that are in a sequence or any other iterable item. Objects that we've learned about that we can iterate over include strings,lists,tuples, and even built in iterables for dictionaries, such as the keys or values.

Explain the concept of a Generator

A generator generates objects for an instance, but does not store every instance generated into memory.

Explain the concept of a method

A method is a function that is called on an object. It is the dot notation in python. Methods are in the form: object.method(arg1,arg2,etc...) sometimes no arguement

Convert Celsius to Fahrenheit given the data Celsius = [0,10,20.1, 34.5]

Celsius = [0,10,20.1, 34.5] Fahrenheit = [((9/5)*temp + 32) for temp in Celsius] Fahrenheit

Illustrate the general format for a FOR loop in Python:

For item in object: statements to do stuff

Explain the concept of a list comprehension

List comprehensions allow us to build out lists using a different notation. You can think of it as essentially a one line for loop built inside of brackets. # Grab every letter in string lst = [x for x in 'word'] lst output: ['w', 'o', 'r', 'd']

xrange( ) function in Python 2

Python has built-in range generator called xrange( ). It is recommended to use xrange( ) for FOR Loops in python 2 (memory reasons). In Py2 range( ) outs a list, xrange( ) will generate elements, but not save them in memory.

What are some differences between def statements and lambda expressions?

Some differences between lambdas and def statements include: lambdas are expressions (they are a value), while defs are statements. lambdas can only be one liners lambdas are anonymous — they have no intrinsic names

Write a program that prints the integers from 1 to 100. But for multiples of three print "Fizz" instead of the number, and for the multiples of five print "Buzz". For numbers which are multiples of both three and five print "FizzBuzz".

for num in xrange(1,101): if num % 5 == 0 and num % 3 == 0: print "FizzBuzz" elif num % 3 == 0: print "Fizz" elif num % 5 == 0: print "Buzz" else: print num

Go through the string below and if the length of a word is even print "even!" st = 'Print every word in this sentence that has an even number of letters'

for word in st.split(): if len(word)%2 == 0: print word+" <-- has an even length!"

Use for, split(), and if to create a Statement that will print out words that start with 's': st = 'Print only the words that start with s in this sentence'

for word in st.split(): if word[0] == 's': print word

Explain what WHILE Loops are

while statement will repeatedly execute a single statement or group of statements as long as the condition is true. The reason it is called a 'loop' is because the code statements are looped through over and over again until the condition is no longer met.


Conjuntos de estudio relacionados

Florida Real Estate: Chapter 3 terms

View Set

Week 7- Business Structures and Agency

View Set