PYTHON
Published with SteemPeak
Screenshot by Willi Glenz
SUMMARY
01 PYTHON ............................................................. v6 19-07
07 cheat sheet ........................................................ v3 19-07
13 hello world ........................................................ v1 19-08 new
02 installation ....................................................... v3 19-07
08 console ............................................................ v1 19-07
04 ide .................. [pycharm eclipse thonny atom] ............... v3 19-07
06 keywords ........................................................... v1 19-07
11 strings ............................................................ v1 19-07
09 pep ................................................................ v1 19-07
12 module os .......................................................... v2 19-07
10 module random ...................................................... v1 19-07
03 tutorials .......................................................... v5 19-08
05 sources ............................................................ v2 19-07
#13
13 HELLO WORLD
$ python3
>>> print('Hello world')
>>> quit() # <ctrl-d>
$ mkdir ~/python_projects
$ cd ~/python_projects
$ vim hello_world.py
print('Hello world')
:wq
$ python3 hello_world.py
sources: python.org
... interpreter : docs.python.org/3/tutorial/interpreter.html
... introduction : docs.python.org/3/tutorial/introduction.html#first-steps-towards-programming
12 MODULE OS
import os
print(dir(os))
os.uname()
os.getcwd()
os.system('lsd -l')
os.system('rm Session.vim')
os.system('touch {}'.format('abc.txt'))
os.system(f'touch {"abc.txt"}')
file1 = 'abc.txt'
file2 = 'xyz.txt'
if os.path.isfile(file1):
os.rename(file1, file2)
else:
print('The file', file1,'does not exist')
file = input('File name: ')
if os.path.isfile(file):
print('The file exists.')
else:
os.system(f'touch {file}')
print('The file was created')
file_info = os.stat('~/.bashrc')
print(type(file_info))
print(file_info)
The Python Tutorial : docs.python.org/3/tutorial/index.html
Miscellaneous operating system interfaces : docs.python.org/3.7/library/os.html
11 STRINGS
msg = 'Hello, World!'
print(msg)
print(dir(msg))
print(help(str.upper))
msg = '{}, {}!'.format('Hello', 'World')
print(msg)
var_1 = 'Hello'
var_2 = 'World'
msg = f'{var_1}, {var_2}!'
print(msg)
for i in msg:
print(i, end='')
print(msg[0])
print(msg[0:5])
print(msg[0:5:2])
print(msg[7:])
print(msg[:5])
print(msg[-1])
print(len(msg))
print(msg.lower())
print(msg.upper())
print(msg.count('l'))
print(msg.replace('l', 'x'))
Tutorial : docs.python.org/2/tutorial/introduction.html#strings
Strings : docs.python.org/2/library/strings.html
String-Methods : docs.python.org/2/library/stdtypes.html#string-methods
Format Strings : docs.python.org/2/library/string.html#formatstrings
f-Strings : docs.python.org/3/reference/lexical_analysis.html#f-strings
10 MODULE RANDOM
import random
counter = random.randint(1,100)
number = 1
for i in range(counter):
print(number)
number += 1
Source : youtu.be/GW6DlAjZdks
09 PEP
PEP-008 Style Guide for Python Code .............. python.org/dev/peps/pep-0008/
PEP-257 Docstring Conventions .................... python.org/dev/peps/pep-0257/
Source : python.org/dev/peps/
08 CONSOLE
$ python3
>>> help()
>>> <ctrl-d>
Documentation : docs.python.org/3.7/contents.html
Help : help()
07 CHEAT SHEET
#COMMENTS
#
""" ... """
''' ... '''
#PRINT
print('Hello, World!')
print('Hello, World! ', end='')
print('Hello, World! ', end='\n')
docs.python.org/3.7/library/functions.html#print
#OPERATORS
+ - * / // % **
= += -= *= /= //= **=
#CONSTANTS
True False None
docs.python.org/3.7/library/constants.html
#VARIABLES
x = 64
print('x =', x)
print('x = ' + str(x))
x = 0b1000000
print('x =', x)
#INPUT
inp = input('Input: ')
#IF
if x = 1:
...
elif x > 1:
...
else:
...
#FOR
for i in [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]:
print(i, end=' ')
for i in range(10):
print(i, end=', ')
#WHILE
n = 10
while n > 0:
print(n)
n -= 1
#FUNTIONS
def fib(n):
""" Calculate Fibonacci Numbers """
a, b = 0, 1
while a < n:
print(a, end=' ')
a, b = b, a+b
print()
def fib2(n):
""" Calculate Fibonacci Numbers """
a, b = 0, 1
result = []
while a < n:
result.append(a)
a, b = b, a+b
return result
docs.python.org/3.7/tutorial/controlflow.html
#STRINGS
msg = 'Hello, World!'
print(msg)
print(msg[5])
print(msg[0:5])
print(msg[0:5:2])
print(msg.lower())
#LISTS
dummy_list = []
dummy_list = ['a', 'b', 'c']
dummy_list = [1, 'xyz', 2.0]
print(dummy_list)
print(dummy_list[-1])
#TUPLES
dummy_tuple = ('abc', 'xyz', 123)
print(dummy_tuple)
print(dummy_tuple[-1])
#SETS
dummy_set_0 = {1, 2, 'abc'}
dummy_set_1 = {1, 2, 3, 4, 5}
dummy_set_2 = {1, 7, 9, 11, 5}
print(dummy_set_0)
print(dummy_set_1 | dummy_set_2)
print(dummy_set_1 & dummy_set_2)
print(dummy_set_1 - dummy_set_2)
print(dummy_set_1 ^ dummy_set_2)
#DICTIONARIES
dummy_dict = {}
dummy_dict = {1:7, 2:23, 3:18, 4:9, 99:0}
dummy_dict[1] = 8
del dummy_dict[99]
dummy_dict[5] = 12
print(dummy_dict)
Tutorial : docs.python.org/3.7/tutorial/index.html
Documentation : docs.python.org/3.7/contents.html
Language Reference : docs.python.org/3.7/reference/index.html
Setup and Usage : docs.python.org/3.7/using/index.html
06 KEYWORDS
import keyword
print(keyword.kwlist)
['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']
Source : docs.python.org/3.7/library/keyword.html
05 SOURCES
Jetbrains : jetbrains.com
Python.org : python.org
University of Waterloo : uwaterloo.ca
Wingware : wingware.com
04 IDE
PYCHARM
Homepage : jetbrains.com
Download : jetbrains.com/pycharm/
Documentation : jetbrains.com/pycharm/documentation
Quick Start : jetbrains.com/help/pycharm/quick-start-guide.html
THONNY
Homepage : thonny.org
Installation # apt install thonny
ATOM
Homepage : atom.io
Documentation : atom.io/docs
Manual : flight-manual.atom.io
Installation # snap install atom
ECLIPSE
Homepage : eclipse.org
Plugin (PyDev) : pydev.org
Installation Eclipse # snap install eclipse
Installation PyDev : Eclipse > Help > Eclipse Marketplace
Source : wiki.python.org/moin/IntegratedDevelopmentEnvironments
03 TUTORIALS
Socratica : youtube.com/playlist?list=PLi01XoE8jYohWFPpC17Z-wWhPOSuh8Er-
Python.org : docs.python.org/3/tutorial/index.html
Python from Scratch : open.cs.uwaterloo.ca/python-from-scratch/
Chuck Severance
Videos : youtube.com/playlist?list=PLlRFEj9H3Oj7Bp8-DfGpfAfDBiblRfl5p
Book : py4e.com/book
Free Online Course : py4e.com
Dive Into Python 3 : file:///usr/share/doc/diveintopython3/html/index.html
Python 3 Tutorial (de) : python-kurs.eu/python3_kurs.php
02 INSTALLATION
# snap install pycharm-community --classic
# apt install python3 idle3 spyder3 python-pip diveintopython3
$ which python; python -V
$ which python3; python3 -V
$ python3
$ python -m SimpleHTTPServer
INSTALLATION v3.7.4
$ mkdir ~/tmp; cd ~/tmp;
$ wget https://www.python.org/ftp/python/3.7.4/Python-3.7.4.tar.xz
$ xz --uncompress Python-3.7.4.tar.xz
$ tar -xf Python-3.7.4.tar
$ rm Python-3.7.4.tar
$ cd Python-3.7.4/
$ view README.rst
$ ./configure
$ make
$ make test
# make install
$ rm -r ~/tmp; ls -lisa ~
UPDATE-ALTERNATIVE
$ python<tab><tab>
$ python --version
# update-alternatives --list python
# update-alternatives --install /usr/local/bin/python3 python /usr/bin/python3.7 1
# update-alternatives --config python
# update-alternatives --list python
$ python --version
# update-alternatives --remove python /usr/bin/python2.7
Homepage : python.org
Download : python.org/downloads/release/python-374/
Documentation : docs.python.org/3.7/
01 PYTHON
CheatSheet $ cheat.sh/python/:learn
IDE : PyCharm Spyder3 Wing
Version $ python -V && python3 -V
DIVE-INTO-PYTHON-3
# apt install diveintopython3
$ dpkg -L diveintopython3 | grep -i index
> /usr/share/doc/diveintopython3/html/index.html
> file:///usr/share/doc/diveintopython3/html/index.html
Homepage : python.org
Download : python.org/downloads/
Documentation : docs.python.org/3.7/
home ~ previous