print('Hello World')
Hello World
print('Hello! '*5)
Hello! Hello! Hello! Hello! Hello!
name = 'Batman'
if name == 'Batman':
print('Hello Batman!')
else:
print('Hello World!')
Hello Batman!
print('Hello World!') # Command 0
print('Hello Galaxy!') # Command 1
print('Hello Universe!') # Command 2
Hello World! Hello Galaxy! Hello Universe!
#
to write comments. print('One') # This is a comment
# print('Two') # The intepreter ignores this line
print('Three')
One Three
print('Hello World') # This WILL work
Hello World
Print('Hello World') # This will NOT work
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-40-e8337bbe3070> in <module>() ----> 1 Print('Hello World') # This will NOT work NameError: name 'Print' is not defined
x
is a variable. 'x' is an English letter.¶print('x') # Print the English letter 'x'
x
x = 10
print(x) # Princt the value of the variable x
10
=
is not the same as '=='¶=
is used to make something equal.==
is used to check if something is equal.x = 10 # Make x have the value 10
x == 20 # Tell me if x is equal to 20?
False
x == 10 # Tell me if x is equal to 10?
True
# This WILL work
x = 10
print(x)
10
# This will NOT work
x = 10
print(x)
File "<ipython-input-47-53b0420fc6ee>", line 3 print(x) ^ IndentationError: unexpected indent
Basic Python is limited but can be extended with packages.
Let's understand what this means.
sqrt(4) # Will NOT work because basic Python is limited
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-48-f00f1e21bca0> in <module>() ----> 1 sqrt(4) # Will NOT work because basic Python is limited NameError: name 'sqrt' is not defined
Let's import a package called math
to fix this problem.
import math # Adding(importing) the functions of the 'math' package
math.sqrt(4)
2.0
We can use other packages too. Lets consider the package numpy.
import numpy as np # We are lazy! We type 'np' instead of 'numpy'
np.sqrt(4)
2.0
.
and 'hidden' functions¶The .
shows ownership.
math.sqrt()
means the sqrt()
function belonging to the package math
np.sqrt()
means the sqrt()
function belonging to the package numpy
upper()
a method that belongs to 'letters'¶'My name is Batman'.upper()
'MY NAME IS BATMAN'
split()
a method that belongs to 'letters'¶'My name is Batman'.split()
['My', 'name', 'is', 'Batman']
1234.split() # Will NOT work because split() is not a method available to numbers
File "<ipython-input-55-d1af11342a47>", line 1 1234.split() # Will NOT work. split() is not a method available to numbers ^ SyntaxError: invalid syntax
py_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] # A Python list
np_array = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10] ) # a numpy array
Althoug they look similary each have different features
np_array[np_array > 4] # Show me (subset) numbers more than 4
array([ 5, 6, 7, 8, 9, 10])
py_list[py_list > 4] # Will NOT work
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-58-2e7235c251b1> in <module>() ----> 1 py_list[py_list > 4] # Will NOT work TypeError: '>' not supported between instances of 'list' and 'int'
np_array + 100 # Add 100 to all elements
array([101, 102, 103, 104, 105, 106, 107, 108, 109, 110])
py_list + 100 # Will NOT work
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-60-7487b5debdc5> in <module>() ----> 1 py_list + 100 # Will NOT work TypeError: can only concatenate list (not "int") to list
py_list + [100,200] # Appends the numbers to the list
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 100, 200]
np_array + [100,200] # Will NOT work
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-62-b11ceebbfa9a> in <module>() ----> 1 np_array + [100,200] # Will NOT work ValueError: operands could not be broadcast together with shapes (10,) (2,)
py_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# First item
py_list[0]
1
# Second item
py_list[1]
2
# Last item
py_list[-1]
10
py_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# Start at index 3 (i.e. '4') and give two (5-3) elements
py_list[3:5]
[4, 5]
# Show me 3 elements from the start
py_list[:3]
[1, 2, 3]
# Show me all elements from the index 3
py_list[3:]
[4, 5, 6, 7, 8, 9, 10]
# Show me everything (from start to end)
py_list[:]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
?np.cos