print(1 + 2)
3
You may find these tips helpful if you have programmed in another language such as C++ or Java. When in doubt, read the docs.
If you have never programmed before, skip this and begin the course.
Python formally uses whitespace as part of its syntax. Examples:
You may be used to languages ending logical lines with a semicolon: ;
. Python ends logical lines with a carriage return (a new line).
Compare Java:
with Python:
Where languages like Java use curly braces {
and }
to group statements, Python typically uses indentation.
Compare Java:
with Python:
The above also demonstrates:
#
for
loop syntax is concise
i
and i += 1
are implied++
, use +=1
:
Nesting loops and/or conditions uses multiple levels of indentation:
You can run the Python interpreter from the command line and experiment with code. Open a terminal and simply run python
.
Basic math operators work similarly in Python and Java:
Dividing two integers (or any two numbers) with /
automatically yields a float. Quotient/remainder is available with the quotient //
and remainder %
operators:
These are not constrained to integers:
Operators like +
and *
work like math operators when the things on both sides of them are numbers. They do other things when they’re around strings.
+
concatenates:
*
extends the string, when used with an integer:
The above ‘multiplication’ operation would be nonsensical with a float instead of an int, or with two strings, and trying it will give you an error.
Powers can be raised with **
:
There is also a well-documented math library for things like square roots.
Note: There is nothing wrong with the math
library, but most people use numpy for math these days.
Conditionals are expressed with if
, elif
, and else
:
if
is required.elif
s are allowed.else
s are allowed, and the else
always comes last.Syntax uses colons and indentation, just like loops.
x = 5
if x > 10:
print("x is huge")
elif x < 0:
print("x is negative")
elif x > 0:
print("x can be counted on fingers")
else:
print("x is zero")
x can be counted on fingers
(This is a very silly example.)
Python has types, but variables do not have types at compile time. Compared to Java, this might seem confusing. You can think of variables as pointing to objects that have types (Python also does not, explicitly, have pointers):
Variables can be reassigned without regard to the type the variable is pointing at:
Assigning a variable to another variable assigns it to wherever the other variable is pointing:
Reassigning the other variable doesn’t change the first variable:
Lists are delimited with square brackets [
and ]
.
They are not typed and can contain whatever you want. Or, for the imaginative, they can contain any type of object.
Individual list elements can be accessed via “slicing.” Note that lists are 0-indexed.
Individual elements of a list can be modified:
Lists are collections of references to variables and behave differently with respect to assignment. Specifically, assigning a variable to another variable that references a list will cause both variables to reference the same list object:
Changing x
also changes y
, since they reference the same thing:
Slicing can access more than one element:
Strings can also be sliced:
More details about slicing in the course and the docs.
Lists can be concatenated:
Lists can be extended with append()
Tuples are similar to lists, but they are immutable. They are optionally delimited with parentheses (
and )
While the parentheses are optional, they are typically used.
Hash tables in Python are called dictionaries, or “dicts.” They consist of key-value pairs. They are delimited with curly braces {
and }
.
This dict has one pair: the key is 'sunrise'
(a string) and the value is 126
(an int).
Dict values can be accessed via their keys:
Dict values can also be or added via their keys:
Functions are defined with def
and a colon. Indentation to group statements follows the same conventions you have already seen.
Functions do not have to return anything.
Variables passed to a function are copied into the function and not modified outside of the scope of the function.
Lists passed to a function are passed as references, and are modified outside of the scope of the function.
['sunset', 'sunrise']
This is a decent time to note that you can get into some trouble with functions because nothing in Python is explicitly typed:
No, use Python 3.
Almost certainly not. The docs are comprehensive and searchable.