Python Notes - Part 01

  1. What is a Variable?
    • Variables are used to store values. A string is a series of characters, surrounded by single or double quotes.
    • Example: age = 23
    • The Integer 23 is stored in the Variable age
  2. How is a Variable named?
    Variable names can contain only letters, numbers, and underscores. They can start with a letter or an underscore, but not with a number. For instance, you can call a variable message_1 but not 1_message.

    Spaces are not allowed in variable names, but underscores can be used to separate words in variable names. For example, greeting message works, but greeting message will cause errors.
  3. What are the different data types?
    • String: (Example: “Joey Marley”)
    • Numbers: (Intergers and Floats)
    • Complex numbers:  (2 + 3j)
    • Boolean (True or False)
    • Lists: test =  ['a', 'b', 'c']
    • Tuples: test = (1, 2, 3)
    • Dictionaries: test = {'Name': 'Zara', 'Age': 7, 'Class': 'First'}
  4. What is the order of calculation in Programming?
    PEMDAS
  5. What is an Escape Sequence?
    • A character that allows the following element to not follow a set rule
    • Example: string = ‘I don\’t love Mondays.’
    • Example of Escape Sequences
    • \n and   \t
  6. What is a traceback?
    A record of where the interpreter ran into trouble when trying to execute your code.
  7. How do you Capitalize a string element?
    • Using the title() method
    • string = “jOeY mArLeY”
    • >>>print(string.title())
    • >>>“Joey Marley”
  8. How do you change the case (uppercase, lowercase) of a string element?
    • Uppercase: upper()
    • string = “jOeY mArLeY”
    • >>>print(string.upper())
    • >>>“JOEY MARLEY”

    • Lowercase: lower()
    • string = “jOeY mArLeY”
    • >>>print(string.lower())
    • >>>“joey marley”
  9. What is a Comment ( # ) in coding?
    • Notes left within the code the are not processed
    • Left as reminder/explanations for the code
  10. What is a List?
    • A list is a collection of items in a particular order.
    • In Python, square brackets ( [] ) indicate a list, and individual elements in the list are separated by commas.
    • Items in the List are indexed starting from zero (0, 1, 2, 3) and left to right.
    • Example:
    • list = [‘car’, ‘boat’,  ‘plane’,  ‘train’]
  11. How can you access an item in a List?
    • list = [‘car’, ‘boat’,  ‘plane’,  ‘train’]
    • >>>print(list[1])
    • >>>boat
    • >>>print(list[0])
    • >>>car

    • Negative numbers start from the end of the list (-1, -2, -3, -4) / no zero
    • >>>print(list[-1])
    • >>>train
    • >>>print(list[-2]
    • >>>plane
  12. How do you change/replace an item in a list?
    • list = [‘honda’, ‘yamaha’,  ‘suzuki’]
    • list[1] = ‘toyota’
    • >>>print(list)
    • >>>[‘honda’, ‘toyota’,  “suzuki’]
  13. How can you add an item to a list?
    • Using the append() method
    • list = [‘honda’, ‘yamaha’,  ‘suzuki’]
    • list.append(‘ducati’)
    • >>>print(list)
    • >>>[‘honda’, ‘yamaha’, ‘suzuki’, ‘ducati’]
    • The append() method adds the item to the end of the list
  14. How can you insert an item into a list at a specific location?
    • Using the insert() method
    • list = [‘honda’, ‘yamaha’, ‘suzuki’]
    • list.insert(0, ‘ducati’)
    • >>>print(list)
    • >>>[‘ducati’, ‘honda’, ‘yamaha’, ‘suzuki’]
  15. How can you remove an item from a list at a specific location?
    • Using the del method
    • list = [‘honda’, ‘yamaha’, ‘suzuki’]
    • del list[0]
    • >>>print(list)
    • [‘yamaha’, ‘suzuki’]
  16. How can an item be removed from a list and still be used?
    • Using the pop.() method
    • list = [‘honda’, ‘yamaha’, ‘suzuki’]
    • popped_item = list.pop()
    • >>>print(list)
    • [‘honda’, ‘yamaha’]
    • >>>print(popped_item)
    • >>>’suzuki’
    • If not defined, the pop() method will remove the last item from the list (see above)
    • popped_item = list.pop(1)
    • >>>print(list)
    • [‘honda’, ‘suzuki’]
    • >>>print(popped_item)
    • >>>’yamaha’
  17. How can an item be removed from a list without an index number?
    • Using the remove() method
    • list = [‘honda’, ‘yamaha’, ‘suzuki’, ‘ducati’]
    • list.remove(‘ducati’)
    • >>>print(list)
    • >>>[‘honda’, ‘yamaha’, ‘suzuki’]
  18. How can a list be sorted alphabetically?
    • Using the sort() method
    • list = [‘b’, ‘d’, ‘a’, ‘c’]
    • list.sort()
    • >>>print(list)
    • >>>[‘a’, ‘b’, ‘c’, ‘d’]
    • list cannot be reverted to its original order after the sort() method is used
  19. How can a list order be reversed alphabetically?
    • Using the sort() method with reverse=True in the parentheses
    • list = [‘b’, ‘d’, ‘a’, ‘c’]
    • list.sort(reverse=True)
    • >>>print(list)
    • >>>[‘d’, ‘c’, ‘b’, ‘a’]
    • list cannot be reverted to its original order after reverse sorting
  20. How can a list be sorted temporarily?
    • Using the sorted() method
    • list = [‘b’, ‘d’, ‘a’, ‘c’]
    • >>>print(sorted(list))
    • >>>[‘a’, ‘b’, ‘c’, ‘d’]
    • >>> print(list)
    • >>>[‘b’, ‘d’, ‘a’, ‘c’]
    • Temporary sorting order can also be reversed
    • >>>print(sorted(list, reverse=True))
    • >>>[‘d’, ‘c’, ‘b’, ‘a’]
    • >>>print(list)
    • >>>[‘b’, ‘d’, ‘a’, ‘c’]
  21. How can a list order be reversed permanently?
    • Using the reverse() method
    • list = [‘b’, ‘d’, ‘a’, ‘c’]
    • list.reverse()
    • >>>print(list)
    • >>>[‘c’, ‘a’, ‘d’, ‘b’]
  22. How can you determine the length of a list?
    • Using the len() method
    • list = [‘b’, ‘d’, ‘a’, ‘c’]
    • >>>print(len(list))
    • >>>4
Author
faker1010
ID
342567
Card Set
Python Notes - Part 01
Description
Python Notes
Updated