Chapter 3-Mathematical functions, strings and objects Flashcards
What is a function?
A group of statements that perform a specific task. Python provides a library of functions
Which functions are ALWAYS available in the python interpreter?
eval, input, print and int. You do not have to import these functions to use them.
What do: abs(x) max(x1, x2..) min(x1, x2...) pow(a,b) round(x) round(x,n) do?
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
(This is from chapter 2): What is something like this called +=?
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)
What is a string?
A sequence of characters(includes numbers and text). Python treats strings and characters the same way.
What is an escape sequence?
A backslash ( \ ) followed by a letter or combination of digits.
\n is known as newline(signifies end of line-EOL) (p.69)
What does the print function automatically do? And how do you stop this automatic function from happening?
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 do you convert something into a string?
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)
What is the string concatenation operator?
String concatenation allows users to put together two strings so they become one.
In python, all data
are actually objects
What is a variable in python?
Variables are actually references to an object
What can you do to an object?
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
Explain each line of code:
> > s=’Welcome’
s2=s.upper()
s2
‘WELCOME’
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.
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?
- 12618.98 is assigned to the variable called amount
- The value 0.0013 is assigned to the value interestRate
- amount and interestRate are multiplied together. This new value is assigned to the variable, interest.
- 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 do you slice a string?
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)