Basics
Comments
Comments in Python start with a #. There isn't a "true" mutli-line comment like /**/ in JavaScript.
# This is a comment
# You need multi-line comments?
# Start each line with hashhaiku
Primitive Data Types
Python has four primitive data types
String
Integer
Float
Boolean
type('spam, spam, and spam')
type(3)
type(3.14)
type(True)
type(False)
isinstance('spam, spam, and spam', str)
isinstance(3, int)
isinstance(3.14, float)
isinstance(True, bool)Everything is an object in Python
Variables
To define a variable in Python, type
In Python, variable names:
must start with a letter or underscore
cannot start with a number
contains only alpha-numeric characters and underscors
are case-sensitive
NOTE: The convention for multi-word variable names in Python is snake_case
Variables can be reassigned
Unlike JavaScript, you cannot declare a variable without initialising it with a value. The closest equivalent would be assigning None
String Operators
+
Concatenation
*
Replication
Arithmetic Operators
+
Addition
-
Subtraction
*
Multiplication
/
Division
//
Integer Division
%
Modulus
**
Exponent
Compound Assignment Operators
+=
eggs += 2
eggs = eggs + 2
-=
eggs -= 2
eggs = eggs - 2
*=
eggs *= 2
eggs = eggs * 2
/=
eggs /= 2
eggs = eggs / 2
//=
eggs //= 3
eggs = eggs // 2
%=
eggs %= 2
eggs = eggs % 2
**=
eggs **= 2
eggs = eggs ** 2
Relational/Comparison Operators
==
Equal to
!=
Not equal to
<
Less than
>
Greater than
<=
Less than or equal to
>=
Greater than or equal to
In Python, you can "chain" conditions
Logical Operators
and
or
not
Last updated
Was this helpful?