Python Basics - Chapter 1

Ace your homework & exams now with Quizwiz!

What operator is / ?

Division 22 / 8 2.75

What operator is **?

Exponent 2 ** 3 = 8

What operator is //?

Integer division/floored quotient 22 // 8 2

What should the following two expressions evaluate to? 'spam' + 'spamspam' 'spam' * 3

Both expressions evaluate to the string 'spamspamspam'.

What is String replication?

>>> 'Alice' * 5 'AliceAliceAliceAliceAlice' The * operator is used for multiplication when it operates on two integer or floating-point values. But when the * operator is used on one string value and one integer value, it becomes the string replication operator. Enter a string multiplied by a number into the interactive shell to see this in action. The expression evaluates down to a single string value that repeats the original a number of times equal to the integer value. String replication is a useful trick, but it's not used as often as string concatenation.

What is String concatenation?

>>> 'Alice' + 'Bob' 'AliceBob The expression evaluates down to a single, new string value that combines the text of the two strings.

What is a data type?

A data type is a category for values, and every value belongs to exactly one data type.

What is an assignment statement?

A statement that sets a variable to a specified value.

What is initialization?

A variable is initialized (or created) the first time a value is stored in it.

What is a variable?

A variable is like a box in the computer's memory where you can store a single value. If you want to use the result of an evaluated expression later in your program, you can save it inside a variable.

What is 2 + 2 an example of?

An expression according to Python

This chapter introduced assignment statements, like spam = 10. What is the difference between an expression and a statement?

An expression evaluates to a single value. A statement does not.

What is an expression made up of? What do all expressions do?

An expression is a combination of values and operators. All expressions evaluate (that is, reduce) to a single value.

What operator is %?

Modulus/remainder 22 % 8 = 6

What operator is *?

Multiplication 3 * 5 15

What does the all() function do?

Return True if all elements of the iterable are true (or if the iterable is empty). Equivalent to: def all(iterable): for element in iterable: if not element: return False return True

What does the any() function do?

Return True if any element of the iterable is true. If the iterable is empty, return False. Equivalent to: def any(iterable): for element in iterable: if element: return True return False

What does abs() function do?

Return the absolute value of a number. The argument may be an integer or a floating point number. If the argument is a complex number, its magnitude is returned. If x defines __abs__(), abs(x) returns x.__abs__().

What does lens() function do?

Return the length (the number of items) of an object. The argument may be a sequence (such as a string, bytes, tuple, list, or range) or a collection (such as a dictionary, set, or frozen set).

What is the general precedence in Python?

The ** operator is evaluated first; the *, /, //, and % operators are evaluated next, from left to right; and the + and - operators are evaluated last (also from left to right). You can use parentheses to override the usual precedence if you need to

What does the variable bacon contain after the following code runs? bacon = 20 bacon + 1

The bacon variable is set to 20. The bacon + 1 expression does not reassign the value in bacon (that would need an assignment statement: bacon = bacon + 1).

What does the float() function do?

The float function takes any value and converts it to floating-point form.

What does the input() function do?

The input() function waits for the user to type some text on the keyboard and press ENTER. You can think of the input() function call as an expression that evaluates to whatever string the user typed in. If the user entered 'Al', then the expression would evaluate to myName = 'Al'.

What does the int() function do?

The int function takes any value and converts it to an integer if possible, or it complains otherwise

What does the len() function do?

the function evaluates to an integer the length of a string. It is then passed to print() to be displayed on the screen.

What three functions can be used to get the integer, floating-point number, or string version of a value?

The int(), float(), and str() functions will evaluate to the integer, floating-point number, and string versions of the value passed to them.

Which of the following are operators, and which are values? * 'hello' -88.8 - / + 5

The operators are +, -, *, and /. The values are 'hello', -88.8, and 5.

What is precedence mean Python?

The order of operations of Python math operators is similar to that of mathematics.

What does the print() function do?

The print() function displays the string value inside the parentheses on the screen. print('Hello world!') print('What is your name?') # ask for their name

Which of the following is a variable, and which is a string? spam 'spam'

The string is 'spam'; the variable is spam. Strings always start and end with quotes.

Name three data types

The three data types introduced in this chapter are integers, floating-point numbers, and strings.

Why is eggs a valid variable name while 100 is invalid?

Variable names cannot begin with a number.

What is termination?

When there are no more lines of code to execute, the Python program terminates; that is, it stops running.

Is it a python convention to start variables with a lowercase letter?

Yes, it is.

Are variable names case-sensitive or not case-sensitive?

Yes, they are case-sensitive. I.e, spam, SPAM, Spam and sPaM are four different variables.

What is an assignment operator?

an equal sign

What are some invalid variable names?

i.e, current-balance (Hyphens are not allowed) current balance (Spaces are not allowed) 4account (can't begin with a number) 42 (can't begin with a number) total_$um(special characters like $ are not allowed) 'hello' (special characters like ' are not allowed)

What does the str() function do?

the str() function returns the string form of an argument.


Related study sets

Fundamentals PrepU - Chapter 28: Pain Management

View Set

History 222 study guide John Wood Mr. Rodriguez

View Set

ECO CHAPTER 24 ( 61 - 95 ) END SEC02

View Set

Ch. 17 Somatic Symptom Disorders (3060 Exam 3)

View Set

Chapter 24 Family Protection and Security

View Set

Ch. 26 Zerwekh Emergency Preparedness

View Set