math operations Flashcards

1
Q

sep=” “

A

space between two words

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

end=”\n”

A

end of line

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

int + string
string / integer

A

unsupported operand type– type error

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

string * 2

A

stringstring

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

\n

A

next line

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

"

A

to escape quotation mark

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

sentense.replace

A

will replace a word “ I “ or a character

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

f string

A

string format

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

(x / y)

A

exact division answer

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

x//y

A

only whole number

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

x%y

A

remainder

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

x += 3

A

x = x + 3

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

x -= 3

A

x = x - 3

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

x /= 3

A

x = x / 3

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

x %= 3

A

x = x % 3

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

x //= 3

A

x = x // 3

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

x **= 3

A

x = x ** 3

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

is

A

Returns True if both variables are the same object

x is y

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

in

A

Returns True if a sequence with the specified value is present in the object

x in y

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

thislist = [“apple”, “banana”, “cherry”, “orange”, “kiwi”, “melon”, “mango”]
print(thislist[:4])

A

This example returns the items from the beginning to, but NOT including, “kiwi”:

[‘apple’, ‘banana’, ‘cherry’, ‘orange’]

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

This example returns the items from “cherry” to the end:

thislist = [“apple”, “banana”, “cherry”, “orange”, “kiwi”, “melon”, “mango”]
print(thislist[2:])

A

This example returns the items from “cherry” to the end:

[‘cherry’, ‘orange’, ‘kiwi’, ‘melon’, ‘mango’]

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

thislist = [“apple”, “banana”, “cherry”, “orange”, “kiwi”, “melon”, “mango”]
print(thislist[-4:-1])

A

This example returns the items from “orange” (-4) to, but NOT including “mango” (-1):

[‘orange’, ‘kiwi’, ‘melon’]

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

thislist = [“apple”, “banana”, “cherry”]
thislist[1:3] = [“watermelon”]
print(thislist)

A

Change the second and third value by replacing it with one value:

[‘apple’, ‘watermelon’]

24
Q

thislist = [“apple”, “banana”, “cherry”]
thislist[1:2] = [“blackcurrant”, “watermelon”]
print(thislist)

A

Change the second value by replacing it with two new values:


[‘apple’, ‘blackcurrant’, ‘watermelon’, ‘cherry’]

25
Insert "watermelon" as the third item: thislist = ["apple", "banana", "cherry"] thislist.insert(2, "watermelon") print(thislist)
['apple', 'banana', 'watermelon', 'cherry']
26
To add an item to the end of the list, use the append() method: Using the append() method to append an item: thislist = ["apple", "banana", "cherry"] thislist.append("orange") print(thislist)
['apple', 'banana', 'cherry', 'orange']
27
To insert a list item at a specified index, use the insert() method. The insert() method inserts an item at the specified index: Example Insert an item as the second position: thislist = ["apple", "banana", "cherry"] thislist.insert(1, "orange") print(thislist)
['apple', 'orange', 'banana', 'cherry']
28
The remove() method removes the specified item. Example Remove "banana": thislist = ["apple", "banana", "cherry"] thislist.remove("banana") print(thislist)
['apple', 'cherry']
29
Addition + and subtraction - has the same precedence, and therefor we evaluate the expression from left to right: print(5 + 4 - 7 + 3)
2
30
List Comprehension List comprehension offers a shorter syntax when you want to create a new list based on the values of an existing list. Example: Based on a list of fruits, you want a new list, containing only the fruits with the letter "a" in the name. Without list comprehension you will have to write a for statement with a conditional test inside: Example fruits = ["apple", "banana", "cherry", "kiwi", "mango"] newlist = [] for x in fruits: if "a" in x: newlist.append(x) print(newlist)
['apple', 'banana', 'mango']
31
Condition The condition is like a filter that only accepts the items that valuate to True. Example Only accept items that are not "apple": newlist = [x for x in fruits if x != "apple"]
The condition is like a filter that only accepts the items that valuate to True. ['banana', 'cherry', 'kiwi', 'mango']
32
unary operation
only one value is involved it can be unary + and Unary - examples x =5 y= -3 print(-1.0) # -1 print(-x) # -5 print(-y) # 3
33
string format() The format() method allows you to format selected parts of a string. Add a placeholder where you want to display the price: price = 49 txt = "The price is {} dollars" print(txt.format(price))
The price is 49 dollars
34
Format the price to be displayed as a number with two decimals: txt = "The price is {:.2f} dollars"
The price is 49.00 dollars
35
Close the file when you are finish with it: f = open("demofile.txt", "r") print(f.readline()) f.close()
Hello! Welcome to demofile.txt
35
To open a file for reading it is enough to specify the name of the file:
f = open("demofile.txt") To open the file, use the built-in open() function. The open() function returns a file object, which has a read() method for reading the content of the file:
36
f = open("demofile.txt", "r") print(f.read())
Hello! Welcome to demofile.txt This file is for testing purposes. Good Luck!
37
To write to an existing file, you must add a parameter to the open() function: "a" - Append - will append to the end of the file "w" - Write - will overwrite any existing content f = open("demofile2.txt", "a") f.write("Now the file has more content!") f.close()
Hello! Welcome to demofile2.txt This file is for testing purposes. Good Luck!Now the file has more content! ** Open command with 'a' attribute opens an existing file and appends to that file. ** w attribute creates a new file and writes to it
38
Open the file "demofile3.txt" and overwrite the content: f = open("demofile3.txt", "w") f.write("Woops! I have deleted the content!") f.close() #open and read the file after the overwriting: f = open("demofile3.txt", "r") print(f.read())
Woops! I have deleted the content!
39
Create a New File "x" - Create - will create a file, returns an error if the file exist "a" - Append - will create a file if the specified file does not exist "w" - Write - will create a file if the specified file does not exist
Example Create a file called "myfile.txt": f = open("myfile.txt", "x") Result: a new empty file is created!
40
delete a file
import os os.remove("demofile.txt")
41
Check if File exist:
Check if file exists, then delete it: import os if os.path.exists("demofile.txt"): os.remove("demofile.txt") else: print("The file does not exist")
42
Delete Folder
os.rmdir()
43
reverse a string
"Hello World"[::-1]
44
What function is used to convert a string to an integer?
Use int to convert strings to integers.
45
Math fabs
Return the absolute value of x.
46
math.fmod(x, y)
function fmod() is generally preferred when working with floats, while Python’s x % y is preferred when working with integers.
47
math.frexp(x)
m is a float and e is an integer such that x == m * 2**e exactly
48
math.isnan(x)
Return True if x is a NaN (not a number), and False otherwise.
49
math.isqrt(n)
Return the integer square root of the nonnegative integer n. This is the floor of the exact square root of n, or equivalently the greatest integer a such that a² ≤ n.
50
date.strftime(format)
Return a string representing the date, controlled by an explicit format string. Format codes referring to hours, minutes or seconds will see 0 values.
51
datetime.weekday()
Return the day of the week as an integer, where Monday is 0 and Sunday is 6. The same as self.date().weekday(). See also isoweekday().
52
random.randrange(start, stop[, step])
Return a randomly selected element from range(start, stop, step).
53
random.shuffle(x)
Shuffle the sequence x in place. To shuffle an immutable sequence and return a new shuffled list, use sample(x, k=len(x)) instead.
54
random.choice(seq)¶
Return a random element from the non-empty sequence seq. If seq is empty, raises IndexError.
55
random.sample(population, k, *, counts=None)
Return a k length list of unique elements chosen from the population sequence. Used for random sampling without replacement.