Skip to content

Acry/python-starter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Python GOTO

Simple Python kickoff

About Python

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.

Installing Python on Arch-Linux

Should come with most Linux distributions.

sudo pacman -S python
The python package installs CPython.

VSCode integration

extension

ms-python.python

Enable for workspace only suggested.

Hello World

# first.py
print("Hello World!")
cat = '😻'
print(cat)

run:

python first.py

or press the green arrow:
run

output:
output

Debug

debug

Package Management

pip — The official package installer for Python.
Python Package Index (PyPI)
https://pypi.org/

Other package managers:

  • Anaconda
  • Miniconda

Modules

pip install $some_pkg
pip uninstall $some_pkg

Dependencies

# 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)

module

Comments

# line comment  
""" block comment """

PEP 257 -- Docstring Conventions

Variables

  • Python has no keyword to declare variables.
  • Variable names are case-sensitive.
x = 5
y = "John"
print(type(x))

Constants

You cannot declare a constant.

Types

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

Scalars

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, False
  • str 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 or nil equivalent, every instance of None is of NoneType.

Numeric Types

  • integers
  • floating point numbers
  • complex numbers complex(x, y) | real() | imag()

Integer Literals

<class 'int'>

  • Integers have unlimited precision.

    Decimal 255 Hex 0xff Octal 0o377 Binary 0b11111111

  • bin()

  • hex()

  • int()

  • oct()

Floats

<class 'float'>

  • Floating point numbers are usually implemented using double in C;
import sys
sys.float_info
y = 3.0
x = float(3)

Booleans

<class 'bool'>

Booleans are integers:

x = bool(1)
True
x = True
x = False

Characters

Python does not have a character or char type. All single characters are strings with length one.

Strings

<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)

Collections

  • sequence of objects

Tuple

<class 'tuple'>

  • duplicates
  • immutable
  • ordered
tup1 = ('physics', 'chemistry', 1997, 2000)
tup2 = "a", "b", "c", "d"
tup1[0] # Output: 'physics'

List

<class 'list'>

  • duplicates
  • mutable
  • ordered
my_list = ['foo', 4, 5, 'bar', 0.4]

Set

<class 'set'>

  • immutable
  • unordered and unindexed
  • no duplicates
myset = {"apple", "banana", "cherry"}

Dictionary

<class 'dict'>

  • key:value pairs
  • mutable
  • no duplicates
  • ordered (since Py 3.7)
thisdict = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}

Arrays

No built-in support, but Lists can be used instead.

Structs

No concept of a struct.
You can use classes as data bags.

class MyClass:
    pass


myClass = MyClass()
myClass.foo = 'some'

Functions

// params (input)
def foo(name):

// return values | results ( output)
def my_function(x):
  return 5 * x

Control Flow

if...else

 if x < 0:
     x = 0
     print('Negative changed to zero')
 elif x == 0:
     print('Zero')
 elif x == 1:
     print('Single')
 else:
     print('More')

while loops

index = 0
while index < 10:
    print(index, end=' ')
    index += 1

for loops

words = ['cat', 'mouse', 'dog']
for word in words:
    print(word, len(word))

OOP Concepts

Classes

References

https://www.python.org/

https://wiki.archlinux.org/index.php/python

https://en.wikipedia.org/wiki/Python_(programming_language)

https://code.visualstudio.com/docs/python/python-tutorial

https://docs.python.org/3/reference/

https://wiki.python.org/moin/BeginnersGuide

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages