SI 106 - Definitions and Examples

Ace your homework & exams now with Quizwiz!

how to make a multi-line comment

""" """ (triple quotes)

how to indicate comments

#

If you do not know the number of arguments that will be passed into your function, there is a prefix you can add in the function definition, which prefix? def my_function( kids): print("The youngest child is " + kids[2])

*

If you do not know the number of keyword arguments that will be passed into your function, there is a prefix you can add in the function definition, which prefix? def my_function(kid): print("His last name is " + kid["lname"])

**

printing multiple variables (concatenation) uses which operator?

+

the ?? operator can only concatenate variables of the same type

+

minimum number of spaces for an indent

1

Year of Python's release

1991

How many spaces is a common indent

4

This code will print: x = 5.5 x = int(x) print(x)

5

Use the correct comparison operator to check if 5 is not equal to 10.

5 != 10

example of a complex number in python

5 + 2j

What is unpacking?

Assigning each element in a set to its own variable

The statement below would print a Boolean value, which one? print(10==9)

False

The statement below would print a Boolean value, which one? (print(10 < 9))

False

is 0 True or False?

False

you can combine a string and a number using the "+" operator

False

Inventor of Python

Guido van Rossum

What can a variable name include?

Letters, numbers, underscores

Pascal Case example

MyVariable

what is the return type of print()?

None

what does the continue statement do?

Skips the current iteration of a loop body

String variables can be declared using single quotes OR double quotes

True

print(bool("abc"))

True

what does the append method do?

adds a specified item at the end of a set

what does the add method do?

adds an item to a set (takes a single argument = the value to be added)

The len() function returns what?

an integer

Exit the loop when x is "banana". fruits = ["apple", "banana", "cherry"] for x in fruits: if x == "banana": ????

break

Stop the loop if i is 3. i = 1 while i < 6: if i == 3 ??? i += 1

break

how are variables created

by assigning a value to it

Local variables

can only be used within the function it was created in

Use the clear method to empty the car dictionary. car = { "brand": "Ford", "model": "Mustang", "year": 1964 }

car.clear()

Use the pop method to remove "model" from the car dictionary. (car = { "brand": "Ford", "model": "Mustang", "year": 1964})

car.pop("model")

Add the key/value pair "color" : "red" to the car dictionary. car = { "brand": "Ford", "model": "Mustang", "year": 1964 }

car["color"] = "red"

Change the "year" value from 1964 to 2020. car = { "brand": "Ford", "model": "Mustang", "year": 1964 }

car["year"] = 2020

Create a variable named carname and assign the value Volvo to it.

carname = "Volvo"

how to turn a number from integer to complex

complex()

In the loop, when i is 3, jump directly to the next iteration. (i = 0 while i < 6: i += 1 if i == 3: ???? print(i)

continue

In the loop, when the item value is "banana", jump directly to the next item. fruits = ["apple", "banana", "cherry"] for x in fruits: if x == "banana": ???? print(x)

continue

syntax to create a function named "my_function"

def my_function():

What does the break statement do?

exits the loop

Insert the correct syntax to convert x into a floating point number. x = 5 x = ??

float(x)

Loop through the items in the fruits list. fruits = ["apple", "banana", "cherry"]

for x in fruits: print(x)

example of unpacking

fruits = ["apple", "banana", "cherry"] x, y, z = fruits

Use the add method to add "orange" to the fruits set. fruits = {"apple", "banana", "cherry"}

fruits.add("orange")

Use the discard method to remove "banana" from the fruits set. fruits = {"apple", "banana", "cherry"}

fruits.discard("banana")

Use the insert method to add "lemon" as the second item in the fruits list. fruits = ["apple", "banana", "cherry"]

fruits.insert(1, "lemon")

Use the remove method to remove "banana" from the fruits set (fruits = {"apple", "banana", "cherry"})

fruits.remove("banana")

Use the correct method to add multiple items (more_fruits) to the fruits set. fruits = {"apple", "banana", "cherry"} more_fruits = ["orange", "mango", "grapes"]

fruits.update(more_fruits)

Change the value from "apple" to "kiwi", in the fruits list. fruits = ["apple", "banana", "cherry"]

fruits[0] = "kiwi"

Insert the correct keyword to make the variable x belong to the global scope. def myfunc(): ??? x = "fantastic"

global x

what is the global keyword? (use "x" as variable)

global x

Print "Hello" if a is equal to b, or if c is equal to d.

if a == b or c == d: print("Hello")

Print "1" if a is equal to b, print "2" if a is greater than b, otherwise print "3" (a = 50, b = 10)

if a == b: print("1") elif a > b: print("2") else: print("3")

Print "Hello World" if a is greater than b. (a = 50, b = 10)

if a>b: print("Hello World")

Use the correct membership operator to check if "apple" is present in the fruits object. (fruits = ["apple", "banana"] if "apple" ???? fruits: print("Yes, apple is a fruit!"))

in

What is missing to check if "apple" is present in the fruits set? fruits = {"apple", "banana", "cherry"} if "apple" ?? fruits: print("Yes, apple is a fruit!")

in

What is the insert method?

inserts a specified item into the list at the specified index (takes two arguments = 1st is the index of the item, 2nd is the item name

what does the update method do?

inserts specified items into a set (takes a single argument, the value of the items)

Python's system type

interpreter (code is executed as soon as it's written)

what is special about the discard method?

it does not raise an error if the specified item DNE

what is annoying about the remove method?

it raises an error if the specified item DNE

Variable names can start with

letters or underscores

What is the return type of range()?

list

Camel Case example

myVariable

Execute a function named my_function. def my_function():

my_function()

Snake Case Example

my_variable

Remove the illegal characters in the variable name: 2my-first_name = "John"

myfirst_name = "John"

Use the correct short hand syntax to write the following conditional expression in one line: (if 5 > 2: print("Yes") else: print("No"))

print("Yes") if 5 > 2 else print("No")

the function used to output a variable

print()

Use the get method to print the value of the "model" key of the car dictionary car = { "brand": "Ford", "model": "Mustang", "year": 1964 }

print(car.get("model"))

Use negative indexing to print the last item in the list. fruits = ["apple", "banana", "cherry"]

print(fruits[-1])

Use the correct syntax to print the first item in the fruits tuple. fruits = ("apple", "banana", "cherry")

print(fruits[0])

Print the second item in the fruits list. fruits = ["apple", "banana", "cherry"]

print(fruits[1])

Use a range of indexes to print the third, fourth, and fifth item in the list. fruits = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"]

print(fruits[2:5])

what does the remove method do?

removes an element from a set (takes a single argument, the name of the element) (no return value)

what does the pop method do?

removes an element from a set (takes a single argument, the name of the element) (returns the value of the element in case it needs to be stored)

what does the strip method do?

removes any whitespace at the beginning and end of strings

what does the replace method do?

replaces a specified substring with a specified substring (takes 2 arguments = 1st is the substring to be replaced, 2nd is the substring that will take its place)

Let the function return the x parameter + 5. def my_function(x): ????

return x + 5

what does the get method do?

returns the element at a given position (takes one argument = the key name, returns the value name)

What is the return type of input()?

string

casting specifies...

the data type of the variable

why use the global keyword?

to make a variable usable outside of a function

comma separators can print out variables of different types together

true

variables are case-sensitive

true

variables can change types

true

Convert the value of txt to lower case. txt = "Hello World" txt = ??

txt.lower()

Replace the character H with a J. txt = "Hello world" txt = ???

txt.replace("H", "J")

Convert the value of txt to upper case. txt = "Hello World" txt = ??

txt.upper()

Get the characters from index 2 to index 4 txt = "hello world" x = ??

txt[2:5}

find the data type of "John"

type("John")

Print i as long as i is less than 6. (i = 1)

while i < 6: print(i) i += 1

assign the same value "orange" to multiple variables x, y, z on the same line of code

x = y = z = "orange"

Assign x, y, z variables to "orange", "banana" and "cherry" on the same line of code

x, y, z = "orange", "banana", "cherry"


Related study sets

Module 8-3: Exemplar: Rheumatoid Arthritis

View Set

marketing exam 2 ch. 5, marketing exam 2 ch. 6

View Set

Unit 4 Quiz (The Respiratory System)

View Set

Anatomy MCQ: Reproductive and Urinary

View Set

Chap 10 Management of Pain and Discomfort

View Set

Ch. 2 - Livestock Handling and Safety

View Set

Chapter 8: Nail Structure and Growth, Chapter 9: Nail Structure and Growth.

View Set