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
Q

Insert “watermelon” as the third item:

thislist = [“apple”, “banana”, “cherry”]
thislist.insert(2, “watermelon”)
print(thislist)

A

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

26
Q

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)

A

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

27
Q

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)

A

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

28
Q

The remove() method removes the specified item.

Example

Remove “banana”:

thislist = [“apple”, “banana”, “cherry”]
thislist.remove(“banana”)
print(thislist)

A

[‘apple’, ‘cherry’]

29
Q

Addition + and subtraction - has the same precedence, and therefor we evaluate the expression from left to right:

print(5 + 4 - 7 + 3)

A

2

30
Q

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)

A

[‘apple’, ‘banana’, ‘mango’]

31
Q

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”]

A

The condition is like a filter that only accepts the items that valuate to True.

[‘banana’, ‘cherry’, ‘kiwi’, ‘mango’]

32
Q

unary operation

A

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
Q

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))

A

The price is 49 dollars

34
Q

Format the price to be displayed as a number with two decimals:

txt = “The price is {:.2f} dollars”

A

The price is 49.00 dollars

35
Q

Close the file when you are finish with it:

f = open(“demofile.txt”, “r”)
print(f.readline())
f.close()

A

Hello! Welcome to demofile.txt

35
Q

To open a file for reading it is enough to specify the name of the file:

A

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
Q

f = open(“demofile.txt”, “r”)
print(f.read())

A

Hello! Welcome to demofile.txt
This file is for testing purposes.
Good Luck!

37
Q

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()

A

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
Q

open and read the file after the overwriting:

Open the file “demofile3.txt” and overwrite the content:

f = open(“demofile3.txt”, “w”)
f.write(“Woops! I have deleted the content!”)
f.close()

f = open(“demofile3.txt”, “r”)
print(f.read())

A

Woops! I have deleted the content!

39
Q

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

A

Example
Create a file called “myfile.txt”:

f = open(“myfile.txt”, “x”)

Result: a new empty file is created!

40
Q

delete a file

A

import os
os.remove(“demofile.txt”)

41
Q

Check if File exist:

A

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
Q

Delete Folder

A

os.rmdir()

43
Q

reverse a string

A

“Hello World”[::-1]

44
Q

What function is used to convert a string to an integer?

A

Use int to convert strings to integers.

45
Q

Math fabs

A

Return the absolute value of x.

46
Q

math.fmod(x, y)

A

function fmod() is generally preferred when working with floats, while Python’s x % y is preferred when working with integers.

47
Q

math.frexp(x)

A

m is a float and e is an integer such that x == m * 2**e exactly

48
Q

math.isnan(x)

A

Return True if x is a NaN (not a number), and False otherwise.

49
Q

math.isqrt(n)

A

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
Q

date.strftime(format)

A

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
Q

datetime.weekday()

A

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
Q

random.randrange(start, stop[, step])

A

Return a randomly selected element from range(start, stop, step).

53
Q

random.shuffle(x)

A

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
Q

random.choice(seq)¶

A

Return a random element from the non-empty sequence seq. If seq is empty, raises IndexError.

55
Q

random.sample(population, k, *, counts=None)

A

Return a k length list of unique elements chosen from the population sequence. Used for random sampling without replacement.