1 Python Object and Data Structure Basics Flashcards
What are the major python data types?
- integers int whole numbers Ex. 3 400 4
- floating point float numbers with a decimal point Ex. 3.0 4.5 7.887
- strings strordered sequwnced charaters Ex. “hello” “bye” “milk”
- lists list ordered sequwnce of objects Ex. [400,”bob”, 4.00]
- dictionaries dict unordered key:Value pairs Ex. {“name1”:”Bob”,”color”:”red”}
- tuples tup ordered sequwnce of immutable objects v objects Ex. (300,”Bob”,3.0)
- sets set unoreded collection of unique objects Ex. {“a”,”b”}
- booleans bool logical values Ex.
symbol for addtion
+
symbol for subtraction
-
symbol for mult
*
symbol for division
/
1.) symbol for modulo
2.) explain what it does
- %
- Gives you the remainder after division.
Example 7%4 will give you 3
Example 8%2 will give you 0
What is the symbol for exponents?
**
Example 2**3 will give you 8
what is a variable?
variables stores data types
what are some of the rules for naming variables?
- cannot start with a number
- cannot be spaces in the names(use _ intead of spaces)
- cannot use any of these symbols :’’’,<>?|()!@$%^&*^-+
- best practice to keep names lowercase
- avoid speacial names like “list” , “str” , “int” etc
what is dynamtic typing?
you can reassigned variables to different data types
Example:
dog = 5
dog = “Jolly”
print(dog)
the code should output “Jolly”
- what is and symbol of the type function
- what does it do?
- type()
- it tells the type of data type of something
Example: type(5) #it should output int
type(“Bob”) #it should output str
- Define what a string is
- how do you write strings
- Strings are sequence of charaters using the syntax to either signle quotes or double quotes
- Example:“Bob”, ‘pizza’, “ I don’t do that “
what is indexing in terms of strings?
indexing is the term you use when you want to grab a single chararter of a string. we us [] to get there character we want.
Example:
quote = “hello world”
quote[0] #output should be ‘h’
quote[8] #output should be ‘r’
quote[9] #output should be ‘l’
quote[-2] #output should be ‘l’
what is slicing in terms of strings?
slicing allows you to grab a subsection of muluple strings. It uses the following syntax:
[start:stop:step]
start = is the numerical index you want to for the slice start
stop = the index you want to end(but not include)
step = is the size of the “jump” you wish to take or skip by
example:
word = “abcdefghijk”
word[2:] #it should output ‘cdefghijk’
word[2:12] #it should output ‘cdefghijk’
word[:3] #it should output ‘abc’
word[0:3] #it should output ‘abc’
word[3:6] #it should output ‘def’
word[1:3] #it should output ‘bc’
word[::2] #it should output ‘adgj’
word[0:13:2] #it should output ‘adgj’
word[2:7:2] #it should output ‘ceg’
word[0:13:-1] #it should output ‘kjihgfedcba’
word[::-1] #it should output ‘kjihgfedcba’
what is the lenth fucntion in terms of strings?
len()
it tells you the length of a string
Example: len(“bob”) #It should output 3
len(“I am”) #it should output 4
How do you create comments in python?
By using hashtags
Example: #this is a comment
how do you make a string capitalize?
upper method
variable.upper()
Example: name = jason
cap_name = name.upper()
print(name) #should print jason
print(cap_name) #should print JASON
how do you make a string all lower case?
lower method
variable.lower()