knowt logo

Python pt. 2

  • Python uses object-oriented programming

  • Objects are things, or represent things or system. Objects have their own methods and member functions

  • Kinds of objects (data types):

    • String objects:

      • Can be stored within variables, in which case the name of the variable is the object, storing a string literal

      • All values of a string literal are indexed like a list, so if greeting=“Hello Python”, the function print(greeting[6]) will print the letter P, as that is the 7th value of the string literal and indexing values start at 0

      • Just because there are 2 l’s doesn’t meant they are indexed the same, when converting a string literal to a list

      • Using s=greeting[0:7] and then print(s) will print “Hello P”, where s represents a string object.

      • The command to store certain parts of a substring inside a variable can also be written simply as s=greeting[:7] which leaves out the 0. Same thing with the last value, s2=greeting [7:] stores “Python”

      • len() functions may also be used in place of number values

      • Negative numbers may also be used to index the values of a string, with the very last character being indexed as -1, counting down to the left for however many characters there are

      • Although string objects can be treated like lists, they cannot be changed like lists, meaning they are immutable

      • Just like a list, string objects can be used like lists are in loops. For instance,
        greeting=”Hello there!”
        for character in range(len(greeting)):
        print(greeting[character])

      • Built in string object functions:

        • .upper() will convert the whole string to capital letters

        • .title() which will convert only the first letter to a capital and all others to being lowercase

        • .split() will split a string apart, seperating them by whatever is in the parenthesis. If left blank it defaults to a space. If then creates a list of each of the split components. If text=”Hello Python!”, then print(text.split()) will display [‘Hello’,’Python!’]

        • ‘x‘.join(stringObject) is similar to .split, it will join different elements of a list generated from a string object together, using x as an insert between the two, and the list in ()

        • stringObject.replace(‘x’,’y’) will replace any character x with the character y in the string object stringObject

  • Operators:

    • Relational operations:

      • > | Greater than

      • < | Less than

      • >= | Greater than or equal to

      • <= | Less than or equal to

      • == | equal to, not to be confused with =

      • != | not equal to

    • Logical operators:

      • and | This checks if two things are both true

      • or | This checks if either of two things are true

      • not | This is the negation of something

  • If statements:

    • Will only run certain code under the if statement if a certain condition is met

    • If (condition) then (operation) | Will run code if a certain condition is true. Example:
      if favColor=teal:
      print(“Hey that’s my favorite color!”)

    • If (condition) else (operation) | Will run code if a certain condition is false. Example:
      if num/2==2:
      print(“Your number is 4.”)
      else:
      print(“Your number is not 4.”)

Python pt. 2

  • Python uses object-oriented programming

  • Objects are things, or represent things or system. Objects have their own methods and member functions

  • Kinds of objects (data types):

    • String objects:

      • Can be stored within variables, in which case the name of the variable is the object, storing a string literal

      • All values of a string literal are indexed like a list, so if greeting=“Hello Python”, the function print(greeting[6]) will print the letter P, as that is the 7th value of the string literal and indexing values start at 0

      • Just because there are 2 l’s doesn’t meant they are indexed the same, when converting a string literal to a list

      • Using s=greeting[0:7] and then print(s) will print “Hello P”, where s represents a string object.

      • The command to store certain parts of a substring inside a variable can also be written simply as s=greeting[:7] which leaves out the 0. Same thing with the last value, s2=greeting [7:] stores “Python”

      • len() functions may also be used in place of number values

      • Negative numbers may also be used to index the values of a string, with the very last character being indexed as -1, counting down to the left for however many characters there are

      • Although string objects can be treated like lists, they cannot be changed like lists, meaning they are immutable

      • Just like a list, string objects can be used like lists are in loops. For instance,
        greeting=”Hello there!”
        for character in range(len(greeting)):
        print(greeting[character])

      • Built in string object functions:

        • .upper() will convert the whole string to capital letters

        • .title() which will convert only the first letter to a capital and all others to being lowercase

        • .split() will split a string apart, seperating them by whatever is in the parenthesis. If left blank it defaults to a space. If then creates a list of each of the split components. If text=”Hello Python!”, then print(text.split()) will display [‘Hello’,’Python!’]

        • ‘x‘.join(stringObject) is similar to .split, it will join different elements of a list generated from a string object together, using x as an insert between the two, and the list in ()

        • stringObject.replace(‘x’,’y’) will replace any character x with the character y in the string object stringObject

  • Operators:

    • Relational operations:

      • > | Greater than

      • < | Less than

      • >= | Greater than or equal to

      • <= | Less than or equal to

      • == | equal to, not to be confused with =

      • != | not equal to

    • Logical operators:

      • and | This checks if two things are both true

      • or | This checks if either of two things are true

      • not | This is the negation of something

  • If statements:

    • Will only run certain code under the if statement if a certain condition is met

    • If (condition) then (operation) | Will run code if a certain condition is true. Example:
      if favColor=teal:
      print(“Hey that’s my favorite color!”)

    • If (condition) else (operation) | Will run code if a certain condition is false. Example:
      if num/2==2:
      print(“Your number is 4.”)
      else:
      print(“Your number is not 4.”)