JamCoders

💾 Download

JamCoders Day 1 Lecture 1

Boaz Barak

Example

In [25]:
def find_min(L):
    n = len(L)
    current_min = L[0]
    i = 1
    while i < n:
        if L[i]<current_min:
            current_min = L[i]
        i = i + 1
    return current_min
In [32]:
L = [15, 3, 17, 22, 9 , 8]
find_min(L)
Out[32]:
3

Seeing what's going on

The print statement

In [33]:
print("Hello")
Hello
In [34]:
x = 17
print(f"Hello, the variable x is equal to {x}")
Hello, the variable x is equal to 17
In [35]:
print("Hello")
print("World")
Hello
World
In [36]:
print(7, "Banana", x*5)
7 Banana 85

Using print to see how a program funs

In [37]:
def find_min(L):
    n = len(L)
    print(f"Length of L is {n}")
    current_min = L[0]
    print(f"current_min is {current_min}")
    i = 1
    print("Entering loop")
    while i < n:
        print(f"  Loop with i={i}, L[i]={L[i]}")
        if L[i]<current_min:
            print(f"    Updating current_min to {L[i]}")
            current_min = L[i]
        i = i + 1
    print(f"Value of i is {i}")
    print(f"Final output is {current_min}.")
    return current_min
In [38]:
L = [15, 3, 17, 22, 9 , 8]
find_min(L)
Length of L is 6
current_min is 15
Entering loop
  Loop with i=1, L[i]=3
    Updating current_min to 3
  Loop with i=2, L[i]=17
  Loop with i=3, L[i]=22
  Loop with i=4, L[i]=9
  Loop with i=5, L[i]=8
Value of i is 6
Final output is 3.
Out[38]:
3

Jupyter notebook

Notebook composed of cells. Every cell either has text (markdown) or code. If you press Shift+ENTER on a code cell then you run it. The value of the last line is then printed.

In [39]:
# What will be printed
i = 5
i = i + 1
i*2
Out[39]:
12

Use Kernel -> Restart & Clear Output to erase all variables and start from the beginning

Comments

In [ ]:
print("hello") # print("World") This is a comment in the middle of code. It will not run

Variables and types

In [ ]:
x = 5 # integer
y = 6.5 # floating point (number with fraction)
z = "Boaz" # string
w = False 
type(x),type(y),type(z),type(w)
In [ ]:
type('Hello world!')
In [ ]:
print('Hello world!')
In [ ]:
x = 'Hello world!'
print(f"x is a variable of type {type(x)}, the value of x is {x}")
In [ ]:
type( 5 / 2)
In [ ]:
type([0, 1, 2])
In [ ]:
type(5>3)
In [ ]:
# Here is an example of a NoneType
type(None)

Variable assignments

In [ ]:
x = 1
# Putting a variable at the end of a code block will print it
x
In [ ]:
# You can find the type of what the variable holds by using type
type(x)
In [ ]:
# Variables can be reassigned
x = 2
x
In [ ]:
# The name of a variable is not related to the content at all
two = 4
two
In [ ]:
# Variables are case-sensitive
X = 4.
print(X,x)
In [ ]:
# Variables are case-senstive for any part of the variable
Xyz = 3
XYz