Basics Flashcards

1
Q

in one line, give variables x, y, and z their respective values of Orange, Banana, Cherry

Next give the variables the same value of Orange

Next create a list of the fruits, and then give them to the variables

A

x, y, z = “Orange”, “Banana”, “Cherry”

x = y = z = “Orange”

fruits = [ ‘apple’, ‘banana’, ‘cherry’ ]

x, y, z = fruits

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

Variables created outside of a function, so just a normal variable, are called what?

How about a variable that resides in a function?

A

A Global Variable

A Local Variable

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

Create a global variable in a function

A

global x

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

If x has a value and you want to change it in a function to “batman”, how would you do this

A

global x
x = “batman”

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

What are the three numeric types in python?

what does e indicate in this float:
35e3

A

int
float
complex - 1j
Complex numbers are written with ‘j’ as the imaginary part

Power of 10

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

Display a random integer

A

import random

print(random.int(1, 10))
print(random.randrange(1, 10))

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

What is casting?

A

int(variable)

int() is a constructor function

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

Using the print function, see if ‘free’ exists in ‘I am free’

What about seeing if ‘free’ is not in the text?

A

print(‘free’ in txt)

This will return True

if ‘free’ in txt:

if ‘free’ not in txt:

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

What method removes whitespace from the beginning and the end of a string?

A

strip()

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

Replace H with J in “Hello World”

A

x.replace(‘H’, ‘J’)

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

Separate the words by ‘,’ and put them in a list

A

x.split(‘,’)

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

With an f-string, add age = 33 to a string

How would we add two decimal places after that number

A

f’Hello, I am {age:.2f}’

.2f is a formatting type

This would come out like this 33.00

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

Use the escape character to insert double quotes in a double quotes string

A

“We are the "champions"”

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

Use isinstance() to detemine if x = 200 is
and integer True or False

A

print(isinstance(x, int))

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

With memership operators, see if ‘add’ is in ‘add it up’

A

if ‘add’ in x:

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

Change element 2 and three to “water” “smoke”

A

list[3:5] = [‘water’, ‘smoke’]

17
Q

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

thislist = [“apple”, “banana”, “cherry”]

A

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

18
Q

add ‘watermelon’ to the third spot of the list

add ‘fart’ to the end

How would you append elements from another list named tropical to a list named list?

A

list.insert(2, ‘watermelon’)

list.append(‘fart’)

list.extened(tropical) <- works with all other data storage types

19
Q

Remove ‘banana’ from list
Remove it in the form of an index
Delete the list

A

list.remove(‘banana’)
list.pop(2) <- when nothing specified this will delete end element

or

del list[2]

del list

20
Q
A