From b24f917a33e330fcdc223e064f6e6261f13b1313 Mon Sep 17 00:00:00 2001 From: Dor-Ron Date: Mon, 13 Jul 2015 11:56:51 -0400 Subject: [PATCH 1/6] Added Python Loops Added python loops, used parenthesis with print so it would be compatible with both versions 2.x and 3.x even though not practiced in 2.x --- python/loops | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 python/loops diff --git a/python/loops b/python/loops new file mode 100644 index 0000000..f94b7d1 --- /dev/null +++ b/python/loops @@ -0,0 +1,23 @@ +#loops are used to repeat parts of your code. +#There are two main ways to loop in Python, +#Using a FOR loop and a WHILE loop + +#a for loop is written like this +for i in range(1,10): + print(i) + +#make sure to add a comma in between your numbers and a colon at the end of the initial line. +#the range function is exclusive therefore the output would be numbers 1 through 9 not including 10. + +#a while loop is written as follows +x = 10 +y = 0 +while x > y: + print("while loop example") + y += 1 #this iterates variable y every time the loop is runned (same as writing y = y + 1) + +#the result would be outputting "while loop example" ten times, +#until x is no longer greater than y + +#be sure to familiarize yourself with the BREAK and CONTINUE statements as well, +#as they are vital for mastering python's loop control flow. From a75e49dc2cc799d05da2e05c8060c4499576e1c8 Mon Sep 17 00:00:00 2001 From: Dor-Ron Date: Mon, 13 Jul 2015 12:12:22 -0400 Subject: [PATCH 2/6] Added Python Functions Added Python Functions. Used parenthesis around print so its compatible with both 2.x and 3.x. Also used str(input()) instead of raw_input() or just input() on itself for same reason. --- python/functions | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 python/functions diff --git a/python/functions b/python/functions new file mode 100644 index 0000000..a657d10 --- /dev/null +++ b/python/functions @@ -0,0 +1,14 @@ +#the def keyword is used to make a function in python. +def first_hello(): + name = str(input("What is your name? ")) + print("Hello {}".format(name)) + +#functions can take arguments as well. +def second_hello(name): + print("Hello {}".format(name)) + +#to call a function simply simply type the name of the function +#followed by its parameter(s) (if it takes any). + +first_hello() +second_hello("Samuel") # since we have to pass a string for the name argument From b6a984e76ea19c7afdd8d285d5dc7386abf261b8 Mon Sep 17 00:00:00 2001 From: Dor-Ron Date: Mon, 13 Jul 2015 12:29:44 -0400 Subject: [PATCH 3/6] Added Python Objects Added Python objects. Included inheritance. and self argument. used parenthesis for print so it'll be compatible for both python 2.x and 3.x --- python/objects | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 python/objects diff --git a/python/objects b/python/objects new file mode 100644 index 0000000..807ff38 --- /dev/null +++ b/python/objects @@ -0,0 +1,31 @@ +#Everything in python is considered an object. +#To create your own objects you first need to create a class +#which can be done as follows. Remember to indent! + +class Fruit: # class names should always be capitalized + bio = "fruit" # this is a class attribute + + def ready(self, color): # All methods in a class need the self argument + self.color = color + if self.color == "green": + return "The fruit is ripe." + else: + return "The fruit isn't ready yet" + +class Watermelon(Fruit): # The new class inherits everythin from the Fruit class + bio = "watermelon" # overwrite the bio attribute for the watermelon class + +#To make objects simply assign the class to a variable. + +fruit = Fruit() +watermelon = Watermelon() + +#Access object attributes as follows + +print(fruit.bio) # prints out "fruit" +print(watermelon.bio) # prints out "watermelon" + +#To call object methods do + +print(watermelon.ready("green")) # should print out "The fruit is ripe". + # remember Watermelon inherited the ready method from Fruit. From 425b1831c1dbb5e7b0ce4d3df386fcd4f6d14cb9 Mon Sep 17 00:00:00 2001 From: Dor-Ron Date: Mon, 13 Jul 2015 12:30:59 -0400 Subject: [PATCH 4/6] Comment change Added that indentation is required --- python/loops | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/python/loops b/python/loops index f94b7d1..e33c4b8 100644 --- a/python/loops +++ b/python/loops @@ -2,7 +2,7 @@ #There are two main ways to loop in Python, #Using a FOR loop and a WHILE loop -#a for loop is written like this +#a for loop is written like this, the spaces are mandatory for i in range(1,10): print(i) @@ -12,7 +12,7 @@ for i in range(1,10): #a while loop is written as follows x = 10 y = 0 -while x > y: +while x > y: # once again, indentation is required print("while loop example") y += 1 #this iterates variable y every time the loop is runned (same as writing y = y + 1) From 4f197b2507ede6a4be95c2b3baf542c9d32863ae Mon Sep 17 00:00:00 2001 From: Dor-Ron Date: Mon, 13 Jul 2015 12:31:53 -0400 Subject: [PATCH 5/6] Changed Comment Added that indentation is mandatory --- python/functions | 2 ++ 1 file changed, 2 insertions(+) diff --git a/python/functions b/python/functions index a657d10..3d5a666 100644 --- a/python/functions +++ b/python/functions @@ -1,4 +1,6 @@ #the def keyword is used to make a function in python. +#code that is part of the function needs to be indented four spaces + def first_hello(): name = str(input("What is your name? ")) print("Hello {}".format(name)) From 0ba8685b65ce7955d322ff3b2de718603840cb82 Mon Sep 17 00:00:00 2001 From: Dor-Ron Date: Mon, 13 Jul 2015 12:32:56 -0400 Subject: [PATCH 6/6] Update objects spelling and spacing fix --- python/objects | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/python/objects b/python/objects index 807ff38..666fea0 100644 --- a/python/objects +++ b/python/objects @@ -12,8 +12,8 @@ class Fruit: # class names should always be capitalized else: return "The fruit isn't ready yet" -class Watermelon(Fruit): # The new class inherits everythin from the Fruit class - bio = "watermelon" # overwrite the bio attribute for the watermelon class +class Watermelon(Fruit): # The new class inherits everything from the Fruit class + bio = "watermelon" # overwrite the bio attribute for the watermelon class #To make objects simply assign the class to a variable.