Simple Python kickoff
general-purpose, multi-paradigm, interpreted programming language. Dynamic, strong typing. To support developers is aims for reflection.
Reflective programming or reflection is the ability of a process to examine, introspect, and modify its own structure and behavior.
Should come with most Linux distributions.
sudo pacman -S python
The python package installs CPython.
extension
ms-python.python
Enable for workspace only suggested.
# first.py
print("Hello World!")
cat = '😻'
print(cat)
run:
python first.py
pip — The official package installer for Python.
Python Package Index (PyPI)
https://pypi.org/
Other package managers:
- Anaconda
- Miniconda
pip install $some_pkg
pip uninstall $some_pkg
# main.py
import random
import hello
print("Pseudo-Rand:",random.randint(1, 11))
hello.print_hello()
# hello.py
def print_hello():
print("Hello World!")
cat = '😻'
print(cat)
# line comment
""" block comment """
PEP 257 -- Docstring Conventions
- Python has no keyword to declare variables.
- Variable names are case-sensitive.
x = 5
y = "John"
print(type(x))
You cannot declare a constant.
Built-in Data Types
Text Type: str
Numeric Types: int, float, complex
Sequence Types: list, tuple, range
Mapping Type: dict
Set Types: set, frozenset
Boolean Type: bool
Binary Types: bytes, bytearray, memoryview
The commonly used scalar types in Python are:
int
Any integer.float
Floating point number (64 bit precision)complex
Numbers with an optional imaginary component.bool
True, Falsestr
A sequence of characters (can contain unicode characters).bytes
A sequence of unsigned 8-bit entities, used for manipulating binary data.- NoneType (
None
) - Python’s
null
ornil
equivalent, every instance ofNone
is of NoneType.
- integers
- floating point numbers
- complex numbers
complex(x, y)
|real()
|imag()
<class 'int'>
-
Integers have unlimited precision.
Decimal 255 Hex 0xff Octal 0o377 Binary 0b11111111
-
bin()
-
hex()
-
int()
-
oct()
<class 'float'>
- Floating point numbers are usually implemented using double in C;
import sys
sys.float_info
y = 3.0
x = float(3)
<class 'bool'>
Booleans are integers:
x = bool(1)
True
x = True
x = False
Python does not have a character or char
type. All single characters are strings
with length one.
<class 'str'>
- can be initialized by using single or double quotes
- Strings are
Lists
(Arrays)
a = "Hello, World!"
print(a[1])
len(a)
for x in "banana":
print(x)
- sequence of objects
<class 'tuple'>
- duplicates
- immutable
- ordered
tup1 = ('physics', 'chemistry', 1997, 2000)
tup2 = "a", "b", "c", "d"
tup1[0] # Output: 'physics'
<class 'list'>
- duplicates
- mutable
- ordered
my_list = ['foo', 4, 5, 'bar', 0.4]
<class 'set'>
- immutable
- unordered and unindexed
- no duplicates
myset = {"apple", "banana", "cherry"}
<class 'dict'>
- key:value pairs
- mutable
- no duplicates
- ordered (since Py 3.7)
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
No built-in support, but Lists can be used instead.
No concept of a struct.
You can use classes as data bags.
class MyClass:
pass
myClass = MyClass()
myClass.foo = 'some'
// params (input)
def foo(name):
// return values | results ( output)
def my_function(x):
return 5 * x
if x < 0:
x = 0
print('Negative changed to zero')
elif x == 0:
print('Zero')
elif x == 1:
print('Single')
else:
print('More')
index = 0
while index < 10:
print(index, end=' ')
index += 1
words = ['cat', 'mouse', 'dog']
for word in words:
print(word, len(word))
https://wiki.archlinux.org/index.php/python
https://en.wikipedia.org/wiki/Python_(programming_language)
https://code.visualstudio.com/docs/python/python-tutorial