Python (Google) Flashcards
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>
> > > name = kyle
print(Hello, “ + name)
Hello, Kyle
(note the spaces when defining name are important here!)
Use Python to calculate (((1+2) x 3)/4)^5), and show the result
> > > print((((1+2)*3)/4)**5)
57.6650390625
What is the difference between a/b, a//b, and a%b in python?
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
Define the following data types giving examples for each:
- int
- str
- float
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)
Which of the following are allowable variable names in python: Jimbo print 69_ohYeah _Ohyeah Billy-bob
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
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.
> > > 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.
What is implicit conversion?
The action of the Python interpreter automatically converting one data type into another
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!
> > > def print_seconds(hours, minutes, seconds):
minutes = minutes + hours60
total = 60minutes + seconds
print(total)
What function converts a data type to a string?
> > > str(variable)
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)
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
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
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
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
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.