~$ This is an offline reference manual for learning Python. Within is reference materials for learning the necessary basics to get started in python. Portions excluded are for primarily server development. SyncThing takes care of the hosting, and this folder should be accessible from connected devices.
~$ Hello World is a basic sanity test for all beginning programmers.
print("Hello World")
~$ Add variables to get a result.
x = 5 y = 3 print(x + y)
~$ Subtract variables to get a result.
x = 5 y = 3 print(x - y)
~$ Multiply variables to get a result.
x = 5 y = 3 print(x * y)
~$ Divide variables to get a result.
x = 5 y = 3 print(x / y)
~$ Expoentiate variables to get a result.
x = 5 y = 3 print(x ** y)
~$ Modulus variables to get a result.
x = 5 y = 3 print(x % y)
~$ Get the time in Python. There are two main ways to do this.
from time import time, ctime t = time() print(ctime(t))
You can also instruct the system to get the date on your behalf. Will address system calls later.
import os os.system("date")
~$ Python format for if, if else, and if elif else
string = "Hello World" if string == "Hello World": print "And the world says hello back." end
This approach is only good in a Python loop. You'll really need to use elif and else.
Approach One
string = "Hello World" if string == "Hello World": print("String equals hello world.") else: print("String does not equal hello world.") end
Approach Two
string = "Hello World" if string == "Hello World": print("String equals hello world.") elif string == "Sandwhich King": print("String equals sandwhich king.") else: print("String does not equal hello world.") end
~$ Learn method formatting in Python. Methods can be grouped together into seperate methods, and methods within methods used for dynamic ones.
def greeting(): print("Hello World") end g = greeting()
~$ Classes can group sets of methods for specific objects. Classes are usually an onject, and a method refers to itself.
# Classes are always objects class Person(object): # Methods are always things an object does. Refer to self. def greeting(self): print("Person: Hello World\n") # Call class and method. person = Person() person.greeting()
~$ Write python data to file.
file = open("hello.txt", "w") file.write("Hello World")
~$ Write python data to file.
# From Operating System Import File Path import os.path from os import path os.system("clear") # Conditional for file checking if path.exists("hello.txt"): print("This file seems to be accounted for.") else: print("Be careful how you treat your files.") file = open("hello.txt", "w") file.write("Hello World")
~$ Write python result to html page.
import os file = open("output.html", "w") x = 5 y = 3 result = x * y print_result = str(result) file.write("")
" + print_result + "
Compare this with the Ruby version:
output = "Hello World"
open("output.html", "w") { |f|
f.puts"#{output}
"
}
system("w3m index.html")
~$ This is the basic format for a chatbot that asks your name and remembers it.
# Import path from the OS. import os.path from os import path if path.exists("memory/your_name.txt"): bot_name = open("bot_identity/bot_name.txt", "r") bianca = bot_name.read().strip() name_file = open("memory/your_name.txt", "r") your_name = name_file.read() print(bianca + ": Welcome back " + your_name + ".") else: bot_name = open("bot_identity/bot_name.txt", "r") bianca = bot_name.read().strip() your_name = "Blank Slate: " name = input(bianca + ": What is your name? >> ") file = open("memory/your_name.txt", "w") file.write(name) print(bianca + ": Look forward to seeing your again " + name + ".")
This format is flexible enough, that one can quickly repurpose it into different subroutines.
~$ Automatically write certain subroutines. As this involves RuPython, tutorial will take some time.
Coming Soon