Chapter 3-Mathematical functions, strings and objects Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

What is a function?

A

A group of statements that perform a specific task. Python provides a library of functions

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

Which functions are ALWAYS available in the python interpreter?

A

eval, input, print and int. You do not have to import these functions to use them.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q
What do:
abs(x)
max(x1, x2..)
min(x1, x2...)
pow(a,b)
round(x)
round(x,n)
do?
A

abs(x)-returns the abs value of x

max-returns the largest among x1, x2…

min- returns the smallest among x1, x2…

pow(a,b-returns a^b or a**b

round(x)-Returns an integer nearest to x. If x is equally close
to two integers, the even one is returned

round(x,n)-returns the float value rounded to n digits after the decimal point

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

(This is from chapter 2): What is something like this called +=?

A
It is called an augmented assignment operator.Operators like *,+,-,**,//,/ can be combined with the assignment operator.
such as:
%=
**=(exponent assignment)
/=(division operator)
*=(multiplication assignment)
//= (integer division operator)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is a string?

A

A sequence of characters(includes numbers and text). Python treats strings and characters the same way.

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

What is an escape sequence?

A

A backslash ( \ ) followed by a letter or combination of digits.

\n is known as newline(signifies end of line-EOL)
(p.69)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What does the print function automatically do? And how do you stop this automatic function from happening?

A

It automatically prints a newline ( \n ) which causes the output to advance to the next line.

It can be stopped by passing the special argument
end=”anyending string”
So it would look like: print(item, end=”anyendingstring”)
print(“AAA”, end=’ ‘)(–>AAA is followed by a space)
print(“BBB”, end=’***’)
print(“CCC”, end=’pelinisanasshole’)
print(“DDD”, end=’’)—>has no space between it and therefore nothing is printed since it is an empty string.

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

How do you convert something into a string?

A

The str function can convert a number into a string. But it must be equated by a variable on the left.

s=str(3.4)

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

What is the string concatenation operator?

A

String concatenation allows users to put together two strings so they become one.

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

In python, all data

A

are actually objects

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

What is a variable in python?

A

Variables are actually references to an object

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

What can you do to an object?

A

You can perform operations on the object. Operations are known as functions. The functions for objects are known as methods.

Remember methods can only be invoked by specific objects. For example strings can use the method upper(), lower() to capitalize/lowercase a text

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

Explain each line of code:

> > s=’Welcome’
s2=s.upper()
s2
‘WELCOME’

A

the s variable is assigned to the string object, Welcome.

the string function, upper is acted upon the s variable(which carries the string object). This new object is assigned to a new variable called s2.

s2 is inputed and ‘WELCOME’ is printed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q
Explain each line of code:
1>>amount= 12618.98
2>>interestRate=0.0013
3>>interest=amount*interestRate
4>>print("Interest is", format(interest, ".2f"))

What is the interest amount?

A
  1. 12618.98 is assigned to the variable called amount
  2. The value 0.0013 is assigned to the value interestRate
  3. amount and interestRate are multiplied together. This new value is assigned to the variable, interest.
  4. The string “Interest is”, is printed as well as the function format. Format is a string function.

format(item, format specifier)
item is a number or string and format-specifier(which specifies how the item is formatted)
-don’t focus too much on this-

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

How do you slice a string?

A

You write a reference to the variable which contains the string to be sliced, with square brackets within a set of parameters. The square brackets tend to mean index. So you would be slicing from one parameter, to the other.
ex. variable[ i , j ]. The function slices from i, going up to but not including j (so j-1)

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

In this piece of code, variable[ i , j ]

What would happen if I was not included? variable[:j]

What if j is omitted? variable[i:]

A

If i was omitted, the slicing would start from index 0.

If j was omitted, then the program would extend to the end of the string

17
Q
What is the output of each code?
#1st output
wirt="Dmitri Dmitriyevich Shostakovich"
greg=wirt[0:8]
print(greg)
#second output
wirt="Dmitri Dmitriyevich Shostakovich"
greg=wirt[20:]
print(greg)
#third output
wirt="Dmitri Dmitriyevich Shostakovich"
greg=wirt[:10]
print(greg)
#fourth output
wirt="Dmitri Dmitriyevich Shostakovich"
greg=wirt[:100]
print(greg)
A

second output

#1st output
Dmitri D
If future Kaiyi got it wrong: each character(including spaces make up one index. And since the parameter is [0:8], the string sliced is from 0 up to but not including 8. 

Shostakovich
Since the second part of the parameter is not indicated, the string is sliced to the very end

Dmitri Dmi
Since the first parameter is not indicated, python slices from index 0 up to but not including 10

#fourth output
Dmitri Dmitriyevich Shostakovich
Since the number of characters is less than 100, all the characters are printed(and possibly whatever amount of indexes remain, the invisible spaces