Python is a Programming Language created by Guido van Rossum.
Data Structures
Lists
# Declare a list
l = [] # or l = list()
# Add to a list
l.append("first")
# Insert at desired location
l.insert(1, "second")
# Get length of a list
len(l)
# Remove an item
l.remove("second")
# Check if an item is in our list
if "first" in l:
print("Yes!")
# Get an item
l[0] # Get first item in the list
# Pop an item
l.pop() # Will pop the last
l.pop(0) # Will pop at index "0"
# Copy a list
new_list = l.copy()
# Delete the list
del lDictionaries
Sets
Script Template
#!/usr/bin/env python3
def main():
pass
if __name__ == '__main__':
main()
Under the hood
Python translates itself to a machine code:
# Define a simple function:
def update_position(x, v, dt):
return x + v*dt
import dis # Import the disassembler
dis.dis(update_position) # Disassemble our function
2 0 RESUME 0
3 2 LOAD_FAST 0 (x)
4 LOAD_FAST 1 (v)
6 LOAD_FAST 2 (dt)
8 BINARY_OP 5 (*)
12 BINARY_OP 0 (+)
16 RETURN_VALUE
See also: