One of the simplest things you can do with python is begin to use it as a calculator as so:
3+3
6
You can use all the standard operators:
-
$+$ (add) -
$-$ (subtract -
$*$ (multiply) -
$/$ (divide) -
$**$ (raise to an exponent) -
$%$ (remainder; modular arithmetic) -
$//$ (quotient; floor division)
5-2
3
5+2
7
5*2
10
5/2
2.5
5**2
25
5%2 #Remainder/modular arithmetic
1
5//2 #Quotient/floor division
2
We can use the =
sign to assign values to a variable.
width = 5
width
5
height = 4
width * height
20
If a variable is not defined, you wil get an error
depth
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-14-15a3e886aba1> in <module>()
----> 1 depth
NameError: name 'depth' is not defined
Strings are python's built in data type for storing text. Keep in mind, that digits can also be stored as strings, but will behave differently when stored as strings rather then float or integer numbers.
'Monty Python and the Holy Grail' #single (or double) quotes indicate a string
'Monty Python and the Holy Grail'
"Monty Python and the Holy Grail"
'Monty Python and the Holy Grail'
sentence = "Monty Python \nand the Holy Grail" #\n is a special character denoting "new line"
sentence
'Monty Python \nand the Holy Grail'
print(sentence)
Monty Python
and the Holy Grail
2+2
4
'2'+'2'
'22'
'blah '*5
'blah blah blah blah blah '
ls st*
string-slicing.png
name = 'Monty Python'
name[0] #first character in the string
'M'
name[:5] #first 5 characters in the string
'Monty'
name[:-10] #up to the last 10 characters in the string (note \n counts as 1 character)
'Mo'
name[-6:] #The last 6 characters
'Python'
name[::-1] #Everything going backwards
'nohtyP ytnoM'
Strings also have a number of built in methods including:
- str.lower()
- str.upper()
- str.split()
- str.replace()
- str.strip()
name.lower()
'monty python'
name.upper()
'MONTY PYTHON'
name.split() #This returns a list! We'll talk about that next. (By default splits on spaces)
['Monty', 'Python']
name.split('y') #You can also split by other characters
['Mont', ' P', 'thon']
name.replace('n', 'z')
'Mozty Pythoz'
name.strip() #Remove beginning/ ending whitespace
'Monty Python'
Python also has a number of compound data types such as lists. These are used to group items together.
numbers = [1,2,3,4,5,6,7,8,9,10]
numbers
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
type(numbers)
list
Like strings, lists can be sliced by the index:
numbers[0]
1
numbers[-1]
10
numbers[:5]
[1, 2, 3, 4, 5]
Lists also have many built in methods including:
- list.reverse()
- Doesn't directly return anything but updates the list
- list.pop()
- Remove the item at the given index (7) and return its value
- list.remove()
- Find the given item and remove it. Doesn't return anything directly.
- list.append()
- Append an item to the end of a list. No return value.
- list.insert()
- Insert an item at a specific position.
numbers = [1,2,3,4,5,6,7,8,9,10]
numbers.sort()
numbers
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
numbers.reverse() #Doesn't directly return anything but updates the list
numbers
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
numbers.pop(7) #Remove the item at the given index (7) and return its value
3
numbers
[10, 9, 8, 7, 6, 5, 4, 2, 1]
numbers.remove(7) #Find the given item and remove it. Doesn't return anything directly.
numbers
[10, 9, 8, 6, 5, 4, 2, 1]
numbers.append(7)
numbers
[10, 9, 8, 6, 5, 4, 2, 1, 7]
numbers.insert(-3, 3)
numbers
[10, 9, 8, 6, 5, 4, 3, 2, 1, 7]
Lists are a natural place to start looking at how we can iterate in python.
for number in numbers:
print(number)
1
2
3
4
5
6
7
8
9
10
for num in numbers:
print(num+2)
3
4
5
6
7
8
9
10
11
12
#Notice that we can use any alias as the variable name for our iterator:
for super_bad_variable_name in numbers:
print(super_bad_variable_name)
1
2
3
4
5
6
7
8
9
10
Another basic data type we have ignored up until now are booleans which are used in classical logic. Booleans are either True or False.
type(True)
bool
type(False)
bool
Booleans are very useful for conditional execution. For example:
for i in [1,2,3,4,5]:
if i < 3:
print(i)
1
2
Through each iteration of the loop, python is evaluating the expression i < 3
and resolving it to a boolean to determine whether or not to execute the following paragraph of code.
1 < 3
True
2 < 3
True
3 < 3
False
4 < 3
False
5 < 3
False