define class class MyClass: x = 5 create object (instance of a class) p1 = MyClass() print(p1.x) init() function - the constructor class Person: def __init__(self, name, age): self.name = name self.age = age # Now lets create a object p1 = Person("Jason", 4) print(p1.name) print(p1.age) Objects also have methods class PersonM: def __init__(self, name, age): self.name = name self.age = age def says_hello(self): return f"{self.name} says hello" p2 = PersonM("Snow", 30) print(p2.
IF a = 10 b = 100 if a > b: print("a {0} is greater than b {1}".format(a, b)) IF .. ELIF c = 20 if a > b: print("a {0} is greater than b {1}".format(a, b)) elif b > c: print("b {0} is greater than c {1}".format(b, c)) IF .. ELSE if a > b: print("a {0} is greater than b {1}".format(a, b)) else: print("Whatever") IF with no statement … err b = 2 if c > b: print("c {0} is greater than b {1}".
Functions simple function def say_hello_world(): """Prints hello world""" print("Hello World") calling function
say_hello_world() simple function with parameter def say_hello(someone): """Says Hello to someone""" print(f"Hello {someone}") calling function
say_hello("Friends") simple function with multiple parameters def say_hello_and_more(someone, and_more): """Says Hello to some and append more text""" print(f"Hello {someone}, {and_more}") calling function
say_hello_and_more("Friends", "GreatDay!") simple function with arbitrary parameters def say_hello_to_many(*people): """Says hello to many people :people: names in tuple """ for name in people: print(f"Hello {name}") calling function
Recently I had the opportunity to share some of my learnings with fellow colleagues. I sincerly thank them and I thought it make sense to share some of my learning here.
Setting up a project
Python 3.7 Virtual Env Python Package Manager Python Basics
Hello World Loops Conditions Lists and Tuples Functions Classes
Traits of clean code Our code is the most detailed representation of the design.
Our ulitmate goal is to make the code as robust as possible And, to write it in a way that minimizes defects or makes them utterly evident, should they occur. Maintainability of code, reducting technical debt, working effectivley in agile development, and managing a successful project.
Code Structure Principles and Error Handling Code Structure Principles Dividing Code by Responsibilities Some parts of software a meant to be called directly by users and some by other parts of the code.
let’s get it out of the way
in terminal run helloworld.py
print("Hello World") To execute
$ python helloworld.py
Collection types List
fruits_list = ["apple", "banana", "cherry"] Tuple
fruits_tuple = ("apple", "banana", "cherry") Set
fruits_set = {"apple", "banana", "cherry"} Printing elements of lists, tuple and sets are similar
print("Print Fruits list") for x in fruits_list: print(x) print("") print("Print Fruits tuple") for x in fruits_tuple: print(x) print("\n\nPrint Fruits Set") for x in fruits_set: print(x) But, they are on similar
print("But is list is equal to tuple {0}", \ "yes" if fruits_list == fruits_tuple else "no") print("\n\ngood lord.
#1. loop through an array
fruits = ["apple", "banana", "cherry", "dragon fruit"] print("Example 1: Printing Fruits") for x in fruits: print(x) Notice there is no begin and end blocks! Indenting implies a block has begun
#2. Loop through a string
a_fruit = "banana" print("Example 2: Banana in loop") for x in a_fruit: print(x) #3. Break a loop
print("Example 3: Break example") print("Fruit Array has these values {0}\n\n".format(fruits)) #^^^^ string interpolation print("If we are going to break at banana we only"\ "should be printing an apple\n") #^^^^ statements that span across multiple lines have backslash for x in fruits: if x == "banana": break print(x) #4 For i = 1 to 10