Python Re‐learning Process - RuhDel/Learning_Journal GitHub Wiki

Variables and Types

Numbers

Python supports two types of numbers:

  • Integers (Whole Numbers)
  • Floating Point Numbers (decimals)
  • Bonus: Complex Numbers

To define each of them, you would do so like:

  • myint = 7
  • myfloat = 7.0

Strings

Strings are defined with either a single quotation (' ') or a double quotation (" "). The difference between the two is that using double quotes makes it easy to include apostrophes.

To define, you would type:

  • mystring = "Balls"
  • belonging = "Rocky's"

Lists

Lists are very similar to arrays. They can contain any type of variable, and they can contain as many variables as you wish. Lists can also be iterated over in a very simple manner.

Here is a list example:

  1. mylist = []
  2. mylist.append(1)
  3. mylist.append(2)

This is the process on how to add values 1 and 2 into my list. (Note that it goes by index values, so in this case, 1 would be considered 0 on the index scale and 2 would be 1)

Basic Operators

Arithmetic Operators

Addition, subtraction, multiplication, and division operators can be used with numbers.

Another operator available is the modulo (%) operator, which returns the integer remainder of the division. dividend % divisor = remainder:

  1. remainder = 11 % 3
  2. print(remainder)
  • This prints out 2 since that is the remainder of 11/3

Using two multiplication symbols makes a power relationship:

  1. squared = 7 ** 2
  2. cubed = 2 ** 3
  3. print(squared)
  4. print(cubed)
  • This prints out 49 and 8.

Using Operators and Strings

Python supports concatenating strings using the addition operator:

  1. helloworld = "hello" + " " + "world"
  2. print(helloworld)
  • This prints out hello world

Python also supports multiplying strings to form a string with a repeating sequence:

  1. lotsofhellos = "hello" * 10
  2. print(lotsofhellos)
  • This prints out hellohellohellohellohellohellohellohellohellohello

Using Operators with Lists

Lists can be joined with the addition operators:

  1. numbers1 = [1, 2]
  2. numbers2 = [3, 4]
  3. all_numbers = numbers1 + numbers2
  4. print(all_numbers)
  • This prints out [1, 2, 3, 4]

Just as in strings, Python supports forming new lists with a repeating sequence using the multiplication operator:

  1. print([1,2,3] * 3
  • This prints out [1, 2, 3, 1, 2, 3, 1, 2, 3]