Python Notes - Part 01

  1. What is a Variable?
    • A location to store information
    • Example: age = 23
    • the Integer 23 is stored in the Variable age
  2. How is a Variable name?
    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)
    • Built-in Functions (numerous)
  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?
    • 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 do you select 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? append() method
    • list = [‘honda’, ‘yamaha’,  ‘suzuki’]
    • 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? insert() method
    • list = [‘honda’, ‘yamaha’, ‘suzuki’]
    • insert(0, ‘ducati’)
    • >>>print(list)
    • >>>[‘ducati’, ‘honda’, ‘yamaha’, ‘suzuki’]
  15. How can you remove an item from a list at a specific location? 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? 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? remove() method
    • list = [‘honda’, ‘yamaha’, ‘suzuki’, ‘ducati’]
    • remove(‘ducati’)
    • >>>print(list)
    • >>>[‘honda’, ‘yamaha’, ‘suzuki’]
  18. How can a list be sorted alphabetically? sort() method
    • list = [‘b’, ‘d’, ‘a’, ‘c’]
    • 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? sort(reverse=True)
    • 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 list be sorted temporarily? Sorted() method
    • list = [‘b’, ‘d’, ‘a’, ‘c’]
    • >>>print(sorted(list))
    • >>>[‘a’, ‘b’, ‘c’, ‘d’]
    • >>> print(list)
    • >>>[‘b’, ‘d’, ‘a’, ‘c’]
    • Temporary sorting 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? 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? len() method
    • list = [‘b’, ‘d’, ‘a’, ‘c’]
    • >>>print(len(list))
    • >>>4
Author
Rainer101
ID
342454
Card Set
Python Notes - Part 01
Description
Python Notes
Updated