added another exercise
This commit is contained in:
parent
73822595d9
commit
f1863493ec
@ -0,0 +1,62 @@
|
||||
#! /bin/python3
|
||||
|
||||
x = 2
|
||||
|
||||
print ("x = 2")
|
||||
print ("x == 2", x == 2)
|
||||
print ("x == 3", x == 3)
|
||||
print ("x != 2", x != 2)
|
||||
|
||||
|
||||
name = "John"
|
||||
age = 35
|
||||
|
||||
if name == "John" and age == 35:
|
||||
print("This is an indented statement, becaues it's inside an if clause.")
|
||||
if name == "John" and age > 33:
|
||||
print("This is printed from an second if statement!")
|
||||
else:
|
||||
print("None of the conditions were true")
|
||||
|
||||
#Now we'll iterate through lists
|
||||
|
||||
listNames = ["John", "Rick", "Morty"]
|
||||
#Remeber the name variable is already set to "John"
|
||||
if name in listNames:
|
||||
print("Found the name 'John' inside the listNames variable!")
|
||||
|
||||
#Now well dive into Code Blocks!!
|
||||
|
||||
statement = False
|
||||
anotherStatement = True
|
||||
|
||||
if statement is True:
|
||||
#this is a code block!
|
||||
print("If statement was true, this would print out. But its set to False.")
|
||||
pass
|
||||
elif anotherStatement is True:
|
||||
#another nested code block under the elif statement!
|
||||
print("\n\nWe have found the variable anotherStatement as True")
|
||||
pass
|
||||
else:
|
||||
print("Nothing has evaluated")
|
||||
pass
|
||||
|
||||
#Now wel start to use the "is" operator
|
||||
|
||||
list1 = [1, 2, 3]
|
||||
list2 = ["one", "two", "three"]
|
||||
list3 = [1, 2, 3]
|
||||
|
||||
print ("\n\nlist1 : ", list1, "\nlist2 : ", list2, "\nlist3 : ", list3)
|
||||
print ("list1 == list2 : ", list1 == list2)
|
||||
print ("list1 is list2 : ", list1 is list2)
|
||||
print ("list1 == list3 : ", list1 == list3)
|
||||
print ("list 1 is list3 : ", list1 is list3)
|
||||
|
||||
|
||||
#now time for the not operator
|
||||
|
||||
print ("\n\nnot False : ", not False)
|
||||
print ("(not False) == False : ", (not False) == False)
|
||||
|
||||
@ -0,0 +1,25 @@
|
||||
#Change the variables in the first section, so that each if statement resolves as True.
|
||||
|
||||
# change this code
|
||||
number = 16
|
||||
second_number = 0
|
||||
first_array = [1,2,3]
|
||||
second_array = [1,2]
|
||||
|
||||
if number > 15:
|
||||
print("1")
|
||||
|
||||
if first_array:
|
||||
print("2")
|
||||
|
||||
if len(second_array) == 2:
|
||||
print("3")
|
||||
|
||||
if len(first_array) + len(second_array) == 5:
|
||||
print("4")
|
||||
|
||||
if first_array and first_array[0] == 1:
|
||||
print("5")
|
||||
|
||||
if not second_number:
|
||||
print("6")
|
||||
20
python/learnpython.org/Basics/chapter-08-Loops/README.md
Normal file
20
python/learnpython.org/Basics/chapter-08-Loops/README.md
Normal file
@ -0,0 +1,20 @@
|
||||
Loops
|
||||
|
||||
There are two types of loops in Python, for and while
|
||||
|
||||
|
||||
Exercise
|
||||
|
||||
Loop through and print out all even numbers from the numbers list in the same order they are received. Don't print any numbers that come after 237 in the sequence.
|
||||
|
||||
numbers = [
|
||||
951, 402, 984, 651, 360, 69, 408, 319, 601, 485, 980, 507, 725, 547, 544,
|
||||
615, 83, 165, 141, 501, 263, 617, 865, 575, 219, 390, 984, 592, 236, 105, 942, 941,
|
||||
386, 462, 47, 418, 907, 344, 236, 375, 823, 566, 597, 978, 328, 615, 953, 345,
|
||||
399, 162, 758, 219, 918, 237, 412, 566, 826, 248, 866, 950, 626, 949, 687, 217,
|
||||
815, 67, 104, 58, 512, 24, 892, 894, 767, 553, 81, 379, 843, 831, 445, 742, 717,
|
||||
958, 609, 842, 451, 688, 753, 854, 685, 93, 857, 440, 380, 126, 721, 328, 753, 470,
|
||||
743, 527
|
||||
]
|
||||
|
||||
# your code goes here
|
||||
50
python/learnpython.org/Basics/chapter-08-Loops/example.py
Normal file
50
python/learnpython.org/Basics/chapter-08-Loops/example.py
Normal file
@ -0,0 +1,50 @@
|
||||
#!/bin/python3
|
||||
|
||||
#for loops
|
||||
|
||||
primes = [2, 3, 5, 7, 11, 13, 17]
|
||||
|
||||
for prime in primes:
|
||||
print(prime)
|
||||
|
||||
|
||||
#Now well play with the range() command
|
||||
print("\n\n Playing with range(5)")
|
||||
for x in range(5):
|
||||
print(x)
|
||||
|
||||
print("\nrange(3,6)")
|
||||
for x in range(3,6):
|
||||
print(x)
|
||||
|
||||
print ("\n Lets print out even numbers using range(2,20,2)")
|
||||
print ("Notice how the first digit includes the index specified, but the second integer EXCLUDES the index specified!")
|
||||
|
||||
for x in range(2,20,2):
|
||||
print(x)
|
||||
|
||||
#Now time to play with while loops
|
||||
print("\n\nNow it's time for while loops!")
|
||||
|
||||
count = 0
|
||||
while count <= 10:
|
||||
print(count)
|
||||
count += 1 #same as count = count + 1
|
||||
|
||||
#Time to initiate a break in the while loop
|
||||
print("\n\nUsing the break command in a while loop")
|
||||
|
||||
count = 0
|
||||
while True:
|
||||
print(count)
|
||||
count += 1
|
||||
if count > 10:
|
||||
print("count variable has exceeded 10!!")
|
||||
break
|
||||
print("\n\nLets print only Odd numbers")
|
||||
for x in range(30):
|
||||
#here we check if x is even
|
||||
if x % 2 == 0: #if x divided by two has a remainded or 0, we have an even number!
|
||||
continue
|
||||
print(x)
|
||||
|
||||
18
python/learnpython.org/Basics/chapter-08-Loops/exercise.py
Normal file
18
python/learnpython.org/Basics/chapter-08-Loops/exercise.py
Normal file
@ -0,0 +1,18 @@
|
||||
numbers = [
|
||||
951, 402, 984, 651, 360, 69, 408, 319, 601, 485, 980, 507, 725, 547, 544,
|
||||
615, 83, 165, 141, 501, 263, 617, 865, 575, 219, 390, 984, 592, 236, 105, 942, 941,
|
||||
386, 462, 47, 418, 907, 344, 236, 375, 823, 566, 597, 978, 328, 615, 953, 345,
|
||||
399, 162, 758, 219, 918, 237, 412, 566, 826, 248, 866, 950, 626, 949, 687, 217,
|
||||
815, 67, 104, 58, 512, 24, 892, 894, 767, 553, 81, 379, 843, 831, 445, 742, 717,
|
||||
958, 609, 842, 451, 688, 753, 854, 685, 93, 857, 440, 380, 126, 721, 328, 753, 470,
|
||||
743, 527
|
||||
]
|
||||
|
||||
for i in numbers:
|
||||
if i != 237:
|
||||
if i % 2 == 0:
|
||||
print(i)
|
||||
elif i == 237:
|
||||
break
|
||||
|
||||
|
||||
@ -0,0 +1,5 @@
|
||||
Functions
|
||||
|
||||
What are Functions?
|
||||
|
||||
Functions are a convenient way to divide your code into useful blocks, allowing us to order our code, make it more readable, reuse it and save some time. Also functions are a key way to define interfaces so programmers can share their code.
|
||||
@ -0,0 +1,38 @@
|
||||
#!/bin/python3
|
||||
|
||||
|
||||
def first_function():
|
||||
print("Hello World")
|
||||
print("From inside a funtion")
|
||||
|
||||
first_function()
|
||||
|
||||
|
||||
def second_function(name, age):
|
||||
print("Hello %s, You're getting kind of old at %d. Don't you think?" % (name,age))
|
||||
return name + str(age)
|
||||
|
||||
second_function("John", 35)
|
||||
|
||||
|
||||
#******COPIED FROM THE WEBSITE***
|
||||
print("\n\nEverything from here down was copied and pasted from website!!")
|
||||
# Define our 3 functions
|
||||
def my_function():
|
||||
print("Hello From My Function!")
|
||||
|
||||
def my_function_with_args(username, greeting):
|
||||
print("Hello, %s, From My Function!, I wish you %s"%(username, greeting))
|
||||
|
||||
def sum_two_numbers(a, b):
|
||||
return a + b
|
||||
|
||||
# print(a simple greeting)
|
||||
my_function()
|
||||
|
||||
#prints - "Hello, John Doe, From My Function!, I wish you a great year!"
|
||||
my_function_with_args("John Doe", "a great year!")
|
||||
|
||||
# after this line x will hold the value 3!
|
||||
x = sum_two_numbers(1,2)
|
||||
print(x)
|
||||
@ -0,0 +1,14 @@
|
||||
# Modify this function to return a list of strings as defined above
|
||||
def list_benefits():
|
||||
pass
|
||||
|
||||
# Modify this function to concatenate to each benefit - " is a benefit of functions!"
|
||||
def build_sentence(benefit):
|
||||
pass
|
||||
|
||||
def name_the_benefits_of_functions():
|
||||
list_of_benefits = list_benefits()
|
||||
for benefit in list_of_benefits:
|
||||
print(build_sentence(benefit))
|
||||
|
||||
name_the_benefits_of_functions()
|
||||
Loading…
x
Reference in New Issue
Block a user