Python

Table of Contents

Theory

Programming Style reference

Each programming language contains their own nuances that make up the particular style or flow of the language. These nuance may be;
  • The particular way syntax is used to define a Element (variables/methods/functions/attributes)
  • The naming conventions used for defining abstraction of elements
  • The list of reserved names for elements
  • The syntax used in indentations and loop elements
  • The overall memory management/flow/Input-Output(I/O) that a language is typed in
These styling nuances is what separates the programming language among others along with the intuitive way the language is constructed and its usage. Python as we have learned is known for its simplicity of typing and importing various third party modules to achieve the many goals required that the language is typed in to achieve.

The Author of the Python language, Guido van Rossum along with co-authors of revised python language Barry Warsaw and Nick Coghlan have come up with a styling convention guide to help Python programmers learn to type python code the way Python was designed to be typed. This helps the coder achieve the most realistic expectation of coding in Python closest to how the Python Author themselves would program. Coding using a style guide is by no means a complete way to type as the Inventory of Python themselves, nor as an experienced senior developer, this knowledge comes with years of experience. By learning the Python styling convention and applying it, one may easily start the path to being a veteran developer and make their code more readable by the general public.

Python org, the organisational body of Python language, have compiled the works of the above authors into an online compendium which is constantly updated with new information on how to code in python. This is known as the PEP 8. found on python.comhere

This course recommends reading through the above style guide as it should be the Python coders number one (#1) source of learning Python syntax. If the reader finds the PEP8 link unbearable to read, the summary of important coding conventions are listed below.


Naming convention

When naming elements (the variables/methods/function/attributes) in programming code traditionally there are two (2) ways that many programmers use to define the naming convention. The terminology is named CamelCase. The older term (pre 2000's) is named PascalCase.
Upper CamelCase has each first letter of a combined word capitalised e.g. Camel Case = CamelCase
Lower CamelCase has only the first letter of the second or more combined words capitalised e.g. Camel Case = camelCase
When naming elements in Python the naming convention used are underscores (_) between each word with no Camel/Pascal Case involved e.g. testfile.py = test_file.py

When naming elements, the defined element name cannot exist in the reserved builtin element names. To find the reserved names built into Python 3, run the below commands.

Import builtins
dir(builtins)





Variables

Python intuitively knows the type of a variables without having to declare it furthermore, Variables can be typed without any special syntax involved, only by typing the name of the variable an equal sign (=) to declare the value of the variable, and the value. To then call the variable, the same name has to be typed again, without any syntax considerations.

Example of declaring a value of one (1) to variable x and calling variable x to discover value.
>>>x = 1
>>>x
1


Example of defining two (2) variables and calling the value of both combined variables.
>>>x=1
>>>y=2
>>>z= x + y
>>>z
3


Example of obtaining user input to populate variable and calling same variable.
>>>test_object = input("What is your name?")
>>>Sumeet
>>>print("Your name is, " + test_object)
Your name is, Sumeet



import time

>>>print("Please enter your name")
>>>x = input()
>>>print("Your name is, " + x)
>>>time.sleep(1)
>>>input(" \n Enter key to continue...")



Saving Scripts

Open a notepad file by typing the below 2 lines.

Import os
os.system('notepad')


Save the below text into the notepad and save onto your desktop.

print("Please enter your name")
x = input()
print("Your name is, " + x)
time.sleep(1)
input(" \n Enter key to continue...")


Within py run the file by moving to the directory of the file with

>>>import os
>>>os.chdir('File path of item') e.g. os.chdir('C:\\users\administrator\desktop')


Then simply type the name of the python file to run

>>>test_script.py



Functions

To define functions, first make sure the function name does conflict with existing reserved element names. Start a function by using the built-in def function then declare the name after a space ( ) then place an open and close brackets ( () ) with a colon at the end to start the function. Python doesn't use paranthesises ( { } ) within the python programming language syntax to ease in readability of the code without having the extra overhead of extra unnecessary characters.

After the def function is declared, an indentation of four (4) spaces starts the start of the internal criteria

>>>def test_function():
print("Hello World")
>>>test_function()



To nest commands

>>>import os
>>>website = input("Enter a website starting with www to CURL ")
>>> www.google.com
>>>os.system('curl '+ website)





Pipe Line Output

Context manager for temporarily redirecting sys.stdout to another file or file-like object. This tool adds flexibility to existing functions or classes whose output is hardwired to stdout. For example, the output of help() normally is sent to sys.stdout. You can capture that output in a string by redirecting the output to an io.StringIO object:

>>>f = io.StringIO()
>>>with redirect_stdout(f):
>>> help(pow)
>>>
>>>s = f.getvalue()


To send the output of help() to a file on disk, redirect the output to a regular file:

>>>with open('help.txt', 'w') as f:
>>> with redirect_stdout(f):
>>> help(pow)


To send the output of help() to sys.stderr:

>>>with redirect_stdout(sys.stderr):
>>> help(pow)


Note that the global side effect on sys.stdout means that this context manager is not suitable for use in library code and most threaded applications. It also has no effect on the output of subprocesses. However, it is still a useful approach for many utility scripts.

Visit next page to learn about - Files and Directories