Open-source language, interpreters and ecosystem
Interpreted (but there are tools to compile Python code)
Automatic memory management
Dynamically strongly typed
The function type
returns the type of an object:
type('hello')
str
type(2)
int
type(2.)
float
type(2 + 2)
int
type(2 + 2.)
float
type(True)
bool
Variables are just tags pointing towards objects. New variables can be used when needed. They are not associated with a type but only with an object (which has a type)...
myvar = 1
print(myvar, type(myvar))
1 <class 'int'>
myvar = 'hello'
print(myvar, type(myvar))
hello <class 'str'>
Objects and variables (names) are two very different concepts:
It is often interesting to represent the execution of a Python program in an "object space - namespaces" diagram.
The Zen of Python says "Namespaces are one honking great idea -- let's do more of those!". A namespace is created for every module (file) and for every function execution,
Open-source language, interpreters and ecosystem
Interpreted (but there are tools to compile Python code)
Automatic memory management
Dynamically strongly typed
Gradual learning curve
A philosophy: the Zen of Python
Very clean and readable
Indentation defines the blocks
Style coding is important: pep8
https://www.python.org/dev/peps/pep-0008/
# bad (spaces between operator)
number=0
# ok
number = 0
# bad (indentation with 2 spaces, has to be 4)
if number == 0:
number = 1
# ok
if number == 0:
number = 1
# bad (space after ,)
mylist = [1,2,3]
# ok
mylist = [1, 2, 3]
Open-source language, interpreters and ecosystem
Interpreted (but there are tools to compile Python code)
Automatic memory management
Dynamically strongly typed
Gradual learning curve
A philosophy: the Zen of Python
Very clean and readable
Indentation defines the blocks
Style coding is important: pep8
Python:
The editor can and has to help you!!
In Spyder, open the file ./pyfiles/wrong.py
and see what happens.
Activate the "Real time code style analysis (PEP 8)" (Preferences -> Editor -> Code Introspection/Analysis).
Correct the file to remove the warnings.
Open-source language, interpreters and ecosystem
Interpreted (but there are tools to compile Python code)
Automatic memory management
Dynamically strongly typed
Gradual learning curve
A philosophy: the Zen of Python
Very clean and readable
Indentation defines the blocks
Style coding is important: pep8
Only few keywords and built-in functions
help("keywords")
Here is a list of the Python keywords. Enter any keyword to get more help. False def if raise None del import return True elif in try and else is while as except lambda with assert finally nonlocal yield break for not class from or continue global pass
plus 68 built-in functions... plus few built-in constants... plus built-in exceptions!
Open-source language, interpreters and ecosystem
Interpreted (but there are tools to compile Python code)
Automatic memory management
Dynamically strongly typed
Gradual learning curve
A philosophy: the Zen of Python
Very clean and readable
Indentation defines the blocks
Style coding is important: pep8
Only few keywords and built-in functions
Errors should never pass silently
Multi-paradigm (sequential, object-oriented, functional)
"Batteries Included": the standard library