1: Get Ready For The Python Difference¶
Summary¶
Objects do carry an indication of type, but variables are untyped
Integers in python have an unlimited number od digits
Variables store references to objects, not values
Strings are immutable and support slicing
The list does the work of an array in other languages
The dictionary is essentially an associative array
The tuple is a lightweight immutable list
Code indentation alters the meaning and matters
Classes are objects
Objects have attributes, some of which can be functions
Inheritance works much like other languages, but class objects are directly involved in retrieving attributes
Modules are how Python Code is packed into larger units
Objects¶
Everything is an object
The program (module)
variables (names are just pointers)
functions
classes
Basic Python¶
s = "Strings" # no declaration like "int" or "str"
x = 5
#Math
sum = 5 + 4
sub = 5 - 4
product = 5 * 4
quotient = 5 / 4
remainder = 5 % 4
count = count += 1
#functions
def add(x,y): # def is a python command that tells the program to look at the object that "add" points to
# notice the ":" when a colon is used everything underneath is a part of that line, this is used in loops, functions, and conditionals
return x + y
print(add(2,3)) # prints 5
# can be done with strings as well ex add("lumber", "jack") prints "lumberjack"
Objects in Code¶
class point:
def __init__(self): # the initializer
self.position=(0,0)
def setpos(self,x,y): #setter
self.position=(x,y)
def display(self): #getter
print(self.position)
Modules¶
Python scripts can be an object themselves. Scripts can be imported as modules to other python programs which can access the original scripts objects, or only specific objects
import modulename