Skip to content

Instantly share code, notes, and snippets.

@SergeyParamonov
Last active January 4, 2017 16:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save SergeyParamonov/212dda520f3661af96c9008ab077250b to your computer and use it in GitHub Desktop.
Save SergeyParamonov/212dda520f3661af96c9008ab077250b to your computer and use it in GitHub Desktop.
python exercises
# Intro
# this is a comment: a line starting with the symbol "#" (look on the left)
# it can be ignored, but it helps you to understand the code
# open in a new browser tab the following link
# https://repl.it/languages/python3
# (if what I have written below is too easy for you, go here
# https://github.com/SergeyParamonov/sergeybot/blob/master/list_of_python_warmup_exercsises.txt)
# What is a python program?
# Just a sequence of commands execute from top to down
"command 1"
"command 2"
"command 3"
# commands would be executed from 1 to 3 (with 2 in between)
# Programs we do together
# *First program*:
# just copy (the left column) and execute (push run)
print("Hello World")
# end of the first program
# EXERCISE:
# print the string "I am learning python"
# What happended? After you pushed run?
# Keep in mind "Hello World" -- is a string, or simply text
# *SECOND PROGRAM*
print(42)
print(21 + 21)
print(21*2)
print(84/2)
print(84-42)
print("Hello" + "World")
# so there are strings -- things that have "..." around them, like "Hello" and numbers like 42
# and print(...) -- is a function like f(x) in mathematics, where the thing inside of ( ... ) is input argument
# and "print" is the function name
# EXERCISE:
# print 42 using an arithmetic operator like +, -, /, *, like examples above but not exactly like that!
# *Third program*
x = 0 # is a variable, simply a place where you store a value
# it has the sytnax like <variable name> = <value>, where <value> can be a number or a string
print(21 + x == 42) # "==" is the equality sign in python
# Exercise: make it print true! without changing the print line
# Fourth program
# print your name using a variable x
x = "Sergey"
print("Hello " + x)
# Fifth program
# Ooops, there is a mistake here -- you need to fix it
print(21+21)
print("21" + "21)
# Exercise: fix this program, so it complies
# What did it print and why?
# Sixth program, we want to print
# "Hello 42" and it doesn't work
x = 42
print("Hello " + x)
# Exercise: put a different value into x so that it compiles
# Seventh program
x = 18
y = 20
print(x + y)
# Exercise: make it print 42, without changing the last line
# 8-th program: MAKE X GREAT AGAIN!
# NEW(!) construction, called if-statement
# Related joke:
# A programmer is going to the grocery store and his wife tells him,
# "Buy a gallon of milk, and if there are eggs, buy a dozen."
# So the programmer goes, buys everything, and drives back to his house.
# Upon arrival, his wife angrily asks him, "Why did you get 13 gallons of milk?"
# The programmer says, "There were eggs!"
x = 42
if x > 42:
print("x is so big")
else:
print("x is ok")
# Exercise: modify the program so it prints that x is big
# 9-th program more conditions
x = 42
y = 43
if x > y:
print(" x is bigger than y")
elif x == y:
print(" x is equal to y")
else:
print(" x is smaller than y")
# 10-th tabulation:
# Here we learn why there are spaces at the beginning of lines
x = 42
if x > 11:
print("x is so big")
else:
print("really,")
print("x is so small")
# you see that python uses spaces to say what is inside what
# i.e., we need to put that last line within else-part of if
# Exercise: change the last line so it works as intended
# 10-a exercise (I am running out of numbers!)
# Multiple conditions: if statemets can get multiple conditions at the same time
# == is equal in python and != is not equal
x = 42
y = "Test"
if x > 100 and y != "Test":
print("good job!")
else:
print("something needs to be changed")
# Exercise: modify the condition such that it prints "jood job"
# 12-th program
# a friend of a programmer asks him
# so 0 is false and 1 is true, right?
# 1.
# in python 1 is truth and 0 is false
# in fact, any number but zero is true in python. Why? ¯\_(ツ)_/¯
x = 1
if x:
print("that shouldn't have happened")
else:
print("fine now")
#Exercise: assign another numeric, so it is fine now
# 11-th program cycles
# Imagine you need to print numbers from 1 to 10, each on a new line
# You can easily solve it by doing
print(1)
print(2)
print(3)
print(4)
print(5)
print(6)
print(7)
print(8)
print(9)
print(10)
# so imagine you need to print like from 1 to 100, it is gonna be just stupid, right?
# so we have cycles for that
for x in range(10):
print(x)
# it has the form like
# for <variable> in range(<number>):
# do-stuff-here
# Exercise: print all numbers from 1 to 1000
# 12-th exercise:
# Combinine cycle with variables
# We want to find sum of all numbers from 5 to 100
s = 0 # first, we put zero into s
for x in range(5,25): # x is in the range from 5 to 100 -- arguments of all functions are separated by commas
s = s + x # we add x to s
print(s) # printing the sum
# (just ingore this line, if you just started programming,I know about the last one is not included, not importat for now)
# Exercise: find the sum from 10 to 55
# 13-th exercise
# Combine cycles with ifs
# Find sum from 10 to 100, but only for numbers divisible by 3
s = 0 # so put zero into the sum
for x in range(10,100): # x must be from 10 to 100
if x % 3 == 0: # if x is divisible by 3 i.e. if what is left are divison is zero, then add it!
s = s + x
print(s)
# Exercise: Find the sum from 20 to 110, but only for numbers divisible by 3 and 5
# 14-th exercise: lists
# the range(1,5) is simply a list from 1 to 5, it can be defined explicitely
x = [1,2,3,4,5] # a list of numbers from 1 to 5
y = [10,30,50,60] # a list of numbers 10 and 30 and 50 and 60
for z in x:
print(z)
#Exercise: make it print values from y but only divisible by 3
# program 14-a:
# lists can mix strings and numbers like this
x = [42,"cat",41,40,"dog", "sausage"] # number, string, number, number, string, string
for z in x:
print(z)
# Exercise: print z from x but only if it is not equal to "dog" or "cat" (!= is the symbol for not equal)
# program 14-b: list indices
x = [42, "cat", 41, 40, "dog", "sausage"] # number, string, number, number, string, string
for i in range(5):
print(x[i])
# syntax x[i] -- means, take i-th element of the list x
# i is just a number and x is a list
# Exercise: print elements of x, but only if its index i is divisible by 2
# 15-th exercise: # nested loops
x = [1,2,3,4,5] # a list of numbers from 1 to 5
y = [10,30,50,60] # a list of numbers 10 and 30 and 50 and 60
for z in x:
for h in y:
print(z,h)
# Exercise: print only values z and h (from x and y) such that first is divisible by two and second is divisble by three
# 16-th exercise: functions
# you define functions in python like in mathematics
# f(x) = x * x -- i.e. f(x) is the square of x
# in python
def f(x): # keyword "def" says that what follows is a function name, in (...) you give input parameters
return x * x # return indicates the function's output
y = [1,2,3,4,5]
for z in y:
print(f(z))
# Exercise: print the sum of f(z) for all z in y
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment