C4: Python for Data Science, AI & Development Flashcards
str()
String data type object.
int()
Integer data type object.
float()
Floating Point data type object.
list()
Returns a list of the elements in an object.
E.g.,
Var = “Panks”
list(Var) : (“P”,”a”,”n”,”k”,”s”)
int(3.55):?
3
Here you are casting a floating point through a integer data type, which may have strange results.
float(15):?
15.0
Here you are casting a integer into a floating point data type.
str(4.558):?
“4.558”
Here you are casting a floating point number into a string data type
What are the two Boolean expressions in Python?
True & False*.
They must be camel case.
int(True):?
1
int(False):?
0
bool(1):?
True.
bool(0):?
False.
What is an Operand?
The thing to be operated on.
What is an Operator?
What the Operand opperates on.
type()?
Returns the data type of variable.
Good for debugging.
What’s Typecasting?
Converting one data type into another.
Define Tuple.
1) An Ordered set of numbers.
2) Where the order cannot change.
- The sequence cannot be changed.
- Set is finite.
- Set may contain duplicates.
n-tuple
(X1, … , Xn)
What does RegEx mean?
Regular Expression.
- A Python module.
- A tool for matching and handling strings.
> > > import re
Imports the Regular Expression module built into Python 3.
Concatenation
Combines strings.
Major Data Type Objects in Python.
Integers
Floating Points
Strings
Booleans
Coupoud Data Type Objects? - Python
- Lists
- Tuples
Tuples syntax? - Python
var = (X1,X2,…,…,Xn)
For var = (X1,X2,…,…,Xn),
var[0] returns?
X1
Slicing syntax? - Python
[ n1:n2:n3]
Where:
* n1 = The first index position to return.
* n2 = The last index position to pull, +1.
* n3 = The stride, if any.
Immutable? - Python
Cannot be changed.
E.g.,
* Strings.
* Tuples.
List syntax? - Python
var = [X1,X2,…,…,Xn]
Difference between Lists and Tuples?
Lists are mutable. Tupples are not.
var = [33,55,2,10]
var.extend([9,19])
?
print(var) :
[33,55,2,10,9,19]
var = [33,55,2,10]
var.append([9,19])
?
print(var) :
[33,55,2,10,[9,19]]
var = [33,55,2,10]
del(var[1])
?
var = [33,2,10]
What method would you use to convert a string to a list?
.split()
E.g.,
str(“The rain in spain”)
str2 = str.split()
str2 = [“The”,”rain”,”in”,”spain”]
() is blank, the delimiter is space. Otherwise, pass in a delimiter, such as a colon.
Define Aliasing.
Multiple names refering to the same object.
A = [X1,X2,…,…,Xn]
B = A
list(B) = ?
[X1,X2,…,…,Xn]
A = [X1,X2,…,…,Xn]
B = A[:]
A[2] = 100
list(A) = ?
list(B) = ?
A = [X1,X2,100,…,…,Xn]
B = [X1,X2,…,…,Xn]
A[:] clones the list in variable A.
copy() - Python
A Method used to create a shallow copy of a list.
count() - Python
A Method used to count the number of occurrences of a specific element in a list.
Define these list methods:
Below, let “var”, be any variable that holds a list:
var.pop()
var.reverse()
var.sort
var.remove()
var.insert()
var.count()
var.copy()
-
pop()
method is another way to remove an element from a list in Python. It removes and returns the element at the specified index. If you don’t provide an index to thepop()
method, it will remove and return the last element of the list by default - The
reverse()
method is used to reverse the order of elements in a list - The
sort()
method is used to sort the elements of a list in ascending order. If you want to sort the list in descending order, you can pass thereverse=True
argument to thesort()
method. - To remove an element from a list. The
remove()
method removes the first occurrence of the specified value. - The
count()
method is used to count the number of occurrences of a specific element in a list in Python. - The
copy()
method is used to create a shallow copy of a list.
Dictionaries object syntax?
{'
A':'
a','
B':'
b',...,...,'
N':'
n'}
Dictionary attributes.
- Keys must be unique.
- Keys are immutable.
- –
- Values are mutable
- Values don’t have to be unique.
Assume:{'Key1':'Value1','Key2','Value2',...,...,'KeyN','ValueN'}
.
–
If:Dict['Key2']
:
Value2
Assume:{'Key1':'Value1','Key2','Value2'}
.
–
If:Dict['New1'] = 100
:
{'Key1':'Value1','Key2','Value2','New1':100}
.
Assume:{'Key1':'Value1','Key2':'Value2','New1':100}
.
–
If:del(Dict['Key1'])
:
{‘Key2’:’Value2’,’New1’:100}
Assume:{'
A':'
a','
B':'
b',...,...,'
N':'
n'}
–
Query this Dictionary for B.
'B' in Dict
returns True
Dict.keys()
: ?
This method lists all the keys in the Dictionary.
Dict.values()
: ?
This method lists all the values in the Dictionary.
Set
properties?
- Unordered.
- Containes no duplicate values.
Set
object syntax?
{
3,
1,
2,...,...,
N}
Typecast a List
into a Set
.
set(
list[x,y,z])
Assume:var = [3,58,5,5,10,10,8,6]
–set(var)
: ?
{3,58,5,10,8,6}
Note: A set cannnot contain duplicates. They are removed from the list.
Assume:var = {'lake','fire','water'}
–var.add('smores')
var
: ?
{'lake','fire','water','smores'}
Assume:var = {'lake','fire','water','smores'}
–var.add('fire')
var
: ?
var = {'lake','fire','water','smores'}
*Note: A set cannnot contain duplicates. “fire’ cannot be added.
Assume:var = {'lake','fire','water','smores'}
–var.remove('fire')
var
: ?
var = {'lake','water','smores'}