Data And Datatypes Flashcards

1
Q

Standard DataTypes

A

int
float
str (double or single quotes)
Bool. True/False
None

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

Standard Data Structures

A

List -> ordered Sequence of objects allows duplicates [10,”hello”,200.3] (uses []) SQUARE

Tuples -> Ordered immutable sequence of objects (10,”hello”, 2003) (Uses ()) ROUND

Dictionary -> Unordered Key/Value Pairs {“MyKey”:”Value”, “OtherKey”:”OtherValue”} CURLY

Sets -> Unordered collection of UNIQUE objects. {‘bob’,’bob’,’steve’,’ruby’} CURLY (dupes will be removed)

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

Are Lists Ordered or Unordered

A

Ordered objects which allow duplicates

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

Are Sets Ordered

A

NO. Sets are unordered and UNIQUE. Duplicates are removed automatically.

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

How Does Slicing work?

A

Slicing in Python is a way to extract a subset or subsequence from a list, tuple, string, or other sequence types (like NumPy arrays). It allows you to access a range of elements in a sequence by specifying a start, stop, and step.

myString[2:] 2->End of string
myString[:3] first 3 characters
myString[::2] Start to End stepping every 2
myString[::-1] Reverse a String

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

What methods does a Tuple have?

A

Just 2. count and index.
Tuples are readonly and generally considred faster than Lists so used for static values.

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

How to itterate Dictionary

A

d = {‘key1’: 123, ‘key2’: [0,1,2], ‘key3’:{‘insideKey’:100}}

for k,v in d.items():
print(f”key={k}, value={v}”)

If ‘STEVE’ not in d.keys():
print (“Cant find Steve!”)

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

Main DIctionary Methods

A

Dictionaries can support nested dictionaries:
d = {‘key1’: 123, ‘key2’: [0,1,2], ‘key3’:{‘insideKey’:100}}
d[‘key3’][‘insideKey’]

To add new key/value pair to existing dictionary:
d[‘myOtherKey’] = 500

To add or update multiple items, or another dictionary set, use update. This will add new keys if not found or update existing values for keys that exist.
d.update( {‘key5’: 55, ‘key6’: 66} )

There are two ways to retrieve the value from a dictionary using its key.
d[‘key1’] // Throws an exception if key Not Found
d.get(‘key1’) // Returns None if key Not Found

d.keys()
d.values()
d.items()
d.get(‘STEVE’, ‘Steve Doesnt Exist!’)
del d[‘key1’]

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

String functions

A

len(“steve”) *** note len() is not a method of string

s.split() Split into List of objects - defaults to whitespace unless character(s) passed in,.
s.find(“str”, [start],[end]) Returns the index of a substring (like indexOf). -1 if not found.
s.index(“str”) Same as find but throws ValueError if not found
s.rindex(“str”) Returns LAST occurrence of ‘str’
s.captialize() Convert first character only to Upper Case. This is a sentence.
title() Conver first character for every word to Upper Case. This Is A Sentence.
s.startsWith(“”)
s.endsWith(“”)
s.join(iterator) joins the values of an iterator (keys) using string value as separator

rstrip() Remove spaces at end
lstrip()

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

How to COPY a list

A

a = b[:]

This will copy the contents of list b into a new list a

If we just do a=b, its just pointing ref a to ref b so both refer to same actual list.

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