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:
mylist = []
mylist.append(1)
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:
remainder = 11 % 3
print(remainder)
- This prints out
2
since that is the remainder of 11/3
Using two multiplication symbols makes a power relationship:
squared = 7 ** 2
cubed = 2 ** 3
print(squared)
print(cubed)
- This prints out
49
and8
.
Using Operators and Strings
Python supports concatenating strings using the addition operator:
helloworld = "hello" + " " + "world"
print(helloworld)
- This prints out
hello world
Python also supports multiplying strings to form a string with a repeating sequence:
lotsofhellos = "hello" * 10
print(lotsofhellos)
- This prints out
hellohellohellohellohellohellohellohellohellohello
Using Operators with Lists
Lists can be joined with the addition operators:
numbers1 = [1, 2]
numbers2 = [3, 4]
all_numbers = numbers1 + numbers2
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:
print([1,2,3] * 3
- This prints out
[1, 2, 3, 1, 2, 3, 1, 2, 3]