Basics Flashcards
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
x, y, z = “Orange”, “Banana”, “Cherry”
x = y = z = “Orange”
fruits = [ ‘apple’, ‘banana’, ‘cherry’ ]
x, y, z = fruits
Variables created outside of a function, so just a normal variable, are called what?
How about a variable that resides in a function?
A Global Variable
A Local Variable
Create a global variable in a function
global x
If x has a value and you want to change it in a function to “batman”, how would you do this
global x
x = “batman”
What are the three numeric types in python?
what does e indicate in this float:
35e3
int
float
complex - 1j
Complex numbers are written with ‘j’ as the imaginary part
Power of 10
Display a random integer
import random
print(random.int(1, 10))
print(random.randrange(1, 10))
What is casting?
int(variable)
int() is a constructor function
Using the print function, see if ‘free’ exists in ‘I am free’
What about seeing if ‘free’ is not in the text?
print(‘free’ in txt)
This will return True
if ‘free’ in txt:
if ‘free’ not in txt:
What method removes whitespace from the beginning and the end of a string?
strip()
Replace H with J in “Hello World”
x.replace(‘H’, ‘J’)
Separate the words by ‘,’ and put them in a list
x.split(‘,’)
With an f-string, add age = 33 to a string
How would we add two decimal places after that number
f’Hello, I am {age:.2f}’
.2f is a formatting type
This would come out like this 33.00
Use the escape character to insert double quotes in a double quotes string
“We are the "champions"”
Use isinstance() to detemine if x = 200 is
and integer True or False
print(isinstance(x, int))
With memership operators, see if ‘add’ is in ‘add it up’
if ‘add’ in x: