Data Types and Variables Flashcards
(text) string
“double quotes” and ‘single quotes’
implicit type casting
instead of string f= “fff”
you can do f = “fff” amd the result is the same
Comments
like this
Addition operator + for
a= “lol”
b=”no”
a + b
string concatonation of strings “andy” and “rom”
(combine)
print(“andy” + “rom”)
\n newline for “hello world” (print 3 lines)
print(“Hello World\nHello World\nHello World”)
Can operators have different variable types?
(Ex: a=5 , b=”lol” -> print(a + b)
No, the variable types must match
Alternating string quotes
Ex:String concatonation is done with the “+” sign.
How is this written?
(‘String Concatenation is done with the “+” sign.’)
input() function
input(“What is your name”)
Lets the user enter in a string
comments
single line
multiline
hey whats up
len function
Ex:
len(“fax”)
- string bn = “jams”
len(bn)
- # prints (the length of the string)
3
-#prints(length of the string)
4
Variable
labels for storing information(values)
Operator precedence
PEMDAS
Declaring a variable
Ex: Named result with a value of 3
result = 3
Expression
-Are these expressions?
3 * 3 , result + 4
anything that Python can evaluate to a value
-Yes!
Data type
-Is string and bool a data type?
classes and variables that are usually predefined
-Yes!
What does type() function do?
-Ex:
lmao = 5
type(lmao)
Output: ?
Shows the class or variable type of the variable
-
Variable/data type rules
- What can they include?
- What MUST they start with?
- Lowercase and uppercase letters,numbers and underscores: _
- letter or the underscore character and be case-sensitive
Camel Case
- What is it for real cool person?
- Python naming convention for real cool person ?
Common variable naming convention
- realCoolperson
- Underscore real_cool_person
Escaping(for strings)
-Give an example in this(single quote):
It’s an escaped quote
-Give an example in this(double quotes):
I’m a so-called “scipt kiddie”
-Add a \ BEFORE the ‘ or “ to prevent confusion
‘It's an escaped quote!’
-“I’m a so-called "script kiddie"”
Multiline strings
-Ex:
This is line 1,
… this is line 2,
… this is line 3.
- Triple quotes
”"”This is line 1,
… this is line 2,
… this is line 3.”””
Multiline strings with quotes inside
-Ex: He said: “Hello, I’ve got a question” from the audience
-He said: “Hello, I’ve got a question” from the audience
‘a’ + ‘b’ OUTPUT?
‘a’ - ‘b’ OUTPUT?
- a+b
- Minus with string doesn’t work
mystring = “Hello World”
mystring.lower()
OUTPUT?
hello world
‘Hello world’.split(‘ ‘)
[‘Hello’, ‘world’]
-What does this do?
Splits string into an array with 2 values
Split whitespace
- ‘Hello \t\n there,\t\t\t stranger.’.split() OUTPUT?
- What happened?
[‘Hello’, ‘there,’, ‘stranger.’]
-Each word got split into a list
Replace parts of a string
replace(old, new, # of times)
‘Hello world’.replace(‘H’, ‘h’)
OUTPUT and expain
H was replaced with h
hello world
Reversing a string
mystring = ‘Hello world’
mystring[::-1]
‘dlrow olleH’
Python string format with f-strings
Ex :
my_age = 40
f ‘ ‘ (What goes in the single quotes?)
OUTPUT: My age is 32
f’My age is {my_age - 8}’
- f string allows you to scan for expressions in curly braces {ag }
functions
something you can call, such as an argument
print() funciton
-You can mix
print text to our screen.
-argument types
bool(binary)
method
Ex: title()
An action python can perform on a piece of data
first_name = “ada”
last_name = “lovelace”
fullname =
f string that prints ‘ada lovelace’
fullname =f “{first_name} {last_name} “
print(fullname)
n newline and tab for hello world (3 lines)
print(“New plan:\n\t1.Look at dolphins\n2.Smile\n3.Die”)
Stripping Whitespace
(right)
rstrip()
-Ex: Use this for lol =”python “ to take away the whitespace
(left)
lol = “python”
lol = lol.rstrip()
lol = lol.strip()
Exponents
How would 5^2 and 6^3 be written>?
5 **2
6**3
floats(decimals)
0.2 * 2.0
.4
Intergers and Floats
Defaults to float when mixed with an interger
4/2
8 + 3.0
- 0 decimal
- 0
Underscores in numbers
universenumber= 14_000
14000
Multiple assignment
-Assign x,y and z variable to 0 in the same line
x,y,z= 0,0,0
Constants
(varaible that stays the same throughout the program)
MAX_CON = 100