Python (Google) Flashcards

1
Q

Write a script to demonstrate the syntax of the <i>print<i> function with concatenation; Generate output “Hello, var=name”, define name as Kyle</i></i>

A

> > > name = kyle
print(Hello, “ + name)
Hello, Kyle

(note the spaces when defining name are important here!)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Use Python to calculate (((1+2) x 3)/4)^5), and show the result

A

> > > print((((1+2)*3)/4)**5)

57.6650390625

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the difference between a/b, a//b, and a%b in python?

A

a/b yields the full result of the division, with appropriate decimal places; 56/9=6.222222

a//b yields the whole number answer of the division, as in 56//9=6, leaving out the remainder

a%b yields only the remainder, so 56%9=2, because 56-(9*6)=2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Define the following data types giving examples for each:

  • int
  • str
  • float
A

int = integers, which are whole numbers (ex. 6,9,248)

str = string, which can be text or really anything put in quotation marks (ex. Jim, purple, “84”)

float = real numbers, which are numbers including a decimal (ex. 49.8, -54.2)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q
Which of the following are allowable variable names in python:
Jimbo
print
69_ohYeah
_Ohyeah
Billy-bob
A
Variables can only start with a letter or an underscore, but can contain numbers, letters, and underscores. So:
Good = Jimbo
Nope = print (a python function)
Nope = 69_ohYeah
Good = _Ohyeah
Nope = Billy-bob
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Consider the following example and complete the blanks:

> > > length = 10
width = 2
area = length * width #Line 3
print(“The area is equal to “ + area)

The value 10 is ______ to the variable “_______”. Line 3 is considered an ______ because a result is created when evaluated.

A

> > > length = 10
width = 2
area = length * width #Line 3
print(“The area is equal to “ + area)

The value 10 is ASSIGNED to the variable “LENGTH”. Line 3 is considered an EXPRESSION because a result is created when evaluated.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is implicit conversion?

A

The action of the Python interpreter automatically converting one data type into another

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Create a function “print_seconds” that returns the number of seconds in a time period that requires the arguments ‘hours’, ‘minutes’ and ‘seconds’.

Pay attention to syntax!

A

> > > def print_seconds(hours, minutes, seconds):
minutes = minutes + hours60
total = 60
minutes + seconds
print(total)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What function converts a data type to a string?

A

> > > str(variable)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is wrong with the following:
»>def month_days(month, days):
»> print(month + “ has “ + str(days) + “ days.”)

> > > month_days(June, 30)
month_days(July, 31)

A

month is a variable designed to carry a string, so the arguments “June” and “July” should be within quotation marks; Otherwise python will think these are undefined variables

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q
Define and give the code for the following Boolean operators:
The equality operator
The Inequality operator
The Greater or equal to operator
The lesser than or equal to operator
A

Equality operator “==” : Returns true or false depending on whether the two operands are equal

Inequality operator “!=”: Returns true or false depending on whether the two operands are NOT equal

The Greater or equal to operator “<=”: Returns true or false depending on whether the first operand is bigger than, or equal to the second operand.

The lesser than or equal to operator “>=”: Returns true or false depending on whether the first operand is smaller than, or equal to the second operand.

More at: https://www.tutorialspoint.com/python/python_basic_operators.htm

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What does the following code do? How would we make this do the opposite?

> > > > > > def addition_test(init, increment):
… x = init
… x += increment
… return x

A

This function takes an initial value ‘init’ and adds the value ‘increment’ (i.e. ‘x += 1’ = ‘x = x+1’).

To cause the opposite, we would replace the ‘+=’ operator, with the ‘-=’ operator.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly