Python operations/conversions

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/42

encourage image

There's no tags or description

Looks like no tags are added yet.

43 Terms

1
New cards

addition

+

(e.g 10+5)

2
New cards

subtraction

-

(e.g. 10-5)

3
New cards

multiplication

*

(e.g 10*5 )

4
New cards

division

/

(e.g. 10/5)

5
New cards

Integer division

//

(e.g. 14//5)

(note: always round down, 14//5=2

6
New cards

Power

**

(e.g 2**2)

7
New cards

Modulus

%

(e.g 14%5 will give you 4)

(note: will give you the remainder of a division)

8
New cards

Parentheses

()

(change the order of operations)

9
New cards

Test for equality

==

(e.g. a==b)

(note: will give boolean answer)

10
New cards

Test for inequality

(e.g. a! = b)

(note; will give boolean answer)

11
New cards

Greater/less than

a>b, a<b

(note: will give boolean answer)

12
New cards

Greater/less than or equal to

a>=b, a<=b

(note; will give boolean answer)

13
New cards

converting number to boolean

bool(someData)

(e.g. bool(0) gives False)

14
New cards

converting a float to integer

int(someData)

(e.g. int(7.2) gives 7)

(note; always rounds down so int(7.8) also gives 7)

15
New cards

converting integer to float

float(someData)

(e.g. float(2) gives 2.0)

16
New cards

convert float to string

str(someData)

(e.g. str(17) gives “17”)

17
New cards

convert string to float

float(“someData”)

(e.g. float(“165.4”) gives 165.4)

18
New cards

converting to complex number

complex(someData)

(e.g. complex(5) returns 5+0j)

19
New cards

logical operations overview

performed between boolean variables and result in booleans

20
New cards

Logical “and” operation

value1 and value2

  • Yields True if BOTH value1 or value2 are True

  • Yields False otherwise


21
New cards

Logical “or” operation

value1 or value2

  • Yields True if EITHER value 1 or vlue2 are True

  • Yields False otherwise


22
New cards

Logical “not” operation

not value

  • Yields True if value is False

  • Yields False otherwise


23
New cards

What will addition (+) do to two strings

concatenate them (bring the together)

(e.g. a= “Hi!” b= “How are you?”, a+b returns “Hi! How are you?”

24
New cards

What will multiplication with an integer (*) do to strings

repeat the string multiple times

(e.g. a= “Hi! ”, 4*a returns “Hi! Hi! Hi! Hi! ”)

25
New cards

How would you alter a string

via the object.method()

(e.g. string= “Hi, how are you?”, string.lower() returns “hi, how are you?”)

(note: write your stringName.[then press tab] to open method options)

26
New cards

Does altering a string replace the old string or create new objects

create new objects

27
New cards

What does addition (+) do to lists

concatenates them (adds them together)

(e.g. a=[1,2,3] b=[True, “Bananas”, 17, 28.3], a+b returns [1, 3, 4, True, 'Bananas', 17, 28.3]

28
New cards

What does integer multiplication (*) do to lists

repeats them multiple times

(e.g. a=[1,2,3], 3*a returns [1,2,3,1,2,3,1,2,3])

29
New cards

how do you access elements of a list

aList[index]

(e.g. a=[20,51,-17,3,65,4,812], a[4] returns the 4th term 65)

  • note: Python uses 0 based numbering so 1st element has index 0

  • note: can use negative indices (e.g. a[-1] returns 812)


30
New cards

How would you determine the length of a list

len(aList)

(e.g. a=[20,51,-17,3,65,4,812]

31
New cards

how would you select multiple elements of a list

slicing the list, aList[start:stop:step]

  • start is the first element selected

  • stop is the first element not selected. If none selected will run till end

  • step is the step used between two consecutive elements. If none provided a value of 1 is used

(e.g. a=[1,2,3,4,5,6,7,8,9,10,11,12]

  • a[3:] returns [4, 5, 6, 7, 8, 9, 10, 11, 12]

  • a[0:8:2] returns [1, 3, 5, 7]

  • note: inside the slicing list it is elements not numbers (a[0] returns 1)


32
New cards

How would you add an element to the end of a list

aList.append(someData)

(e.g. a = [1,2,3,4,5,6,7,8,9]

a.append(10)

print(a) returns [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

33
New cards

how would you count the times a number appears in a list

aList.count(number)

34
New cards

how would you remove the first occurrence of an element

aList.remove(element)

35
New cards

how would you reverse the list order

aList.reverse()

36
New cards

how would you sort a list into ascending order

aList.sort()

37
New cards

sort the list in descending order

aList.sort(reverse=True)

38
New cards

removes all contents from a list

aList.clear()

39
New cards

replace an element in a list

aList[0]=newNumber (would replace first element)

40
New cards

how do you determine the type of variable

type(value)


e.g type(“hat”) returns str

type(True) returns bool

type(3) returns int

etc

41
New cards

How do you get the absolute value

abs(number)

(e.g. abs(-9) returns 9

42
New cards

How do you determine the minimum/maximum

min(value1, value2, value3, value4)/max(value1, value2, value3, value4)

min(9,0.001,10,1e-32) returns 1e-32

max(9,0.001,10,1e-32) returns 10

43
New cards

How do you round to nearest integer or specific number of decimal places

round(value), round(value,n)

round(1.8) returns 2

round(1.87636430,5) returns 1.87636 (5dp)