Python Cheat sheet - Pat Cheat Sheet by ZawBlaDE via cheatography.com/25797/cs/6923/ Function print displays inform ation that you want to show on the screen. Police simulator 18 torrentfasrteach. Webex teams demo.
- Naming Conventions During a recent workshop, bootcamp instructor Alex Baransky shared some of the best practices in writing Python code for better readability.Through this series of articles, we will summarize and give you a few examples of those best practices to help you write more elegant Python code, benefiting those who may read and use your code in the future, including yourself.
- Here is my cheat sheet I created along my learning journey. If you have any recommendations (addition/subtraction) let me know. Naming conventions.
- The naming conventions of Python's library are a bit of a mess, so we'll never get this completely consistent - nevertheless, here are the currently recommended naming standards. New modules and packages (including third party frameworks) should be written to these standards, but where an existing library has a different style, internal.
Theory
Programming Style referenceEach 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
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
Python Naming Conventions Cheat Sheet Answers
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.
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
>>>x
1
Example of defining two (2) variables and calling the value of both combined variables.
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
>>>Sumeet
>>>print('Your name is, ' + test_object)
Your name is, Sumeet
Capturing Variable Output
from io import StringIO
import sys
# the below code will temporarily put the output of run commands into the variable sys.stdout. A variable x will be declared with value anything e.g. 10, then printed to screen which will not be output to screen but instead captured in the output object.
old_stdout = sys.stdout
sys.stdout = output = StringIO()
x = 10
print(x)
# reverses output of commands to screen
sys.stdout = old_stdout
print(output.getvalue())
10
import sys
# the below code will temporarily put the output of run commands into the variable sys.stdout. A variable x will be declared with value anything e.g. 10, then printed to screen which will not be output to screen but instead captured in the output object.
old_stdout = sys.stdout
sys.stdout = output = StringIO()
x = 10
print(x)
# reverses output of commands to screen
sys.stdout = old_stdout
print(output.getvalue())
10
import time
>>>import time
>>>print('Please enter your name')
>>>x = input()
>>>print('Your name is, ' + x)
>>>time.sleep(1)
>>>input(' n Enter <ENTER> key to continue..')
>>>print('Please enter your name')
>>>x = input()
>>>print('Your name is, ' + x)
>>>time.sleep(1)
>>>input(' n Enter <ENTER> key to continue..')
Saving Scripts
Open a notepad file by typing the below 2 lines.
Save the below text into the notepad and save onto your desktop.
import time
print('Please enter your name')
x = input()
print('Your name is, ' + x)
time.sleep(1)
input(' n Enter <ENTER> key to continue..')
print('Please enter your name')
x = input()
print('Your name is, ' + x)
time.sleep(1)
input(' n Enter <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:usersadministratordesktop')
>>>os.chdir('File path of item') e.g. os.chdir('C:usersadministratordesktop')
Then simply type the name of the python file to run
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()
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)
>>>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()
>>>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)
>>> with redirect_stdout(f):
>>> help(pow)
To send the output of help() to sys.stderr:
Python Naming Conventions Variables
>>>with redirect_stdout(sys.stderr):
>>> help(pow)
>>> 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.