Python Basics: Keywords, Identifiers and Indention π
Welcome to the first article in my first series ever: "Python: Explain Like I'm Five"!
In this first article, I'll be introducing you to a few beginner Python concepts! π
- reserved keywords
- identifiers & the rules for creating them
- comments
- indention
Strap in, folks. Keep all hands, feet, arms, legs inside the ride. We're about to learn some Python π
Contents
Keywords
Keywords are the reserved words in Python. We can't use these keywords as identifiers (variable names), function names, etc.! They're used to define the syntax & structure of Python. (Watch out-- they're case-sensitive! Notice how only True, None and False are capitalized!)
There are 33 keywords in Python! To access these keywords in your Python shell, simply type
help> keywords
and they'll pop up! Check 'em out below π
Identifiers
A Python identifier is a name used to identify a variable, function, class, module or other object. π
Here are a few rules to remember while creating identifiers:Identifiers can be a combination of letters in lowercase (a to z), uppercase (A to Z), digits (0 to 9) or underscores (_). Names like:
myClass
,var_1
andprint_this_to_screen
, all are valid examples.An identifier cannot start with a digit.
1variable
is invalid, butvariable1
is a valid nameKeywords (in the chart above) cannot be used as identifiers
We also cannot use special symbols like !, @, #, $, % etc. in our identifiers!
Look what happens if you use a keyword as an identifier for a variable β¬οΈ
global = 1
Output:
File "<interactive input>", line 1
global = 1
^
SyntaxError: invalid syntax
Look what happens if use special symbols like !, @, #, $, % etc. in our identifiers! β¬οΈ
a@ = 0
Output:
File "<interactive input>", line 1
a@ = 0
^
SyntaxError: invalid syntax
Tips to remember:
- Python is a case-sensitive language! So
variable
andVariable
aren't the sameπ - Always give identifiers a name that makes sense for what you're creating. While
b = 10
is a valid name, writingbooks = 10
would make more sense, and it would be easier to figure out what it represents when you're looking back at your code (or having others read it!). - Multiple words can be separated using an underscore, like
this_is_an_acceptable_identifier
Statements
Instructions that a Python interpreter can execute are called statements. For example, a = 1
is an assignment statement.
Multi-line statements
In Python, the end of a statement is usually marked by a newline character (\n) like, print("This is using the newline char\n")
. But, we can make a statement extend over multiple lines with the line continuation character (). Here's an example β¬οΈ
a = 1 + 2 + 3 + \
4 + 5 + 6 + \
7 + 8 + 9
In Python, a line continuation is implied inside parentheses (), brackets [], and braces {}. For instance, we can implement the above multi-line statement like so β¬οΈ
a = (1 + 2 + 3 +
4 + 5 + 6 +
7 + 8 + 9)
Here's an example with brackets [] β¬οΈ
colors = ['blue',
'purple',
'orange']
You can also put multiple statements in one line using semicolons β¬οΈ
a = 2; b = 4; c = 6
Comments
Comments are super easy! And very important. Use them to describe exactly what you're doing for anyone who has to look at your source code, as well as for your own personal use.
Here's an example of comments in Python:
#I am
#a comment
print("comments rock")
You can also create multi-line statements! You can use hashtags #
or a set of 3 single or double quotes: ' ' '
or """
Look below β¬οΈ
#An example of
#Multi-line
#comments
An example with quotes:
"""This is also a
perfect example of
multi-line comment"""
Indention
Indention is SO IMPORTANT in Python!
Most programming languages like C, C++, and Java use braces { } to define a block of code. Python uses INDENTATION π
Here's the breakdown: a code block (the body of a function, loop, etc.) starts with indentation and ends with the first unindented line. The amount of indentation is up to you, but it has to be consistent throughout that entire block.
Usually, four whitespaces are used for indentation and are preferred over tabs. Let's look at some examples β¬οΈ
for i in range(1,100):
print(i)
if i == 10:
break
See how nice and clean the above code looks? You can thank white space for that π
Indention can be ignored in single line continuations, like below β¬οΈ
if True: print('Howdy partner'); a = 5
But, doesn't it look a bit unorganized compared to this indented version? β¬οΈ
if True:
print('Howdy partner')
a = 5
You made it to the end! Thanks for reading! π
In Part 2, I'll be going over:
- Variables (Python variables, constants, literals and their use cases)
- Collections (to hold variables, literals, constants, etc. --> Dictionaries, lists, tuples, sets)
I'll be uploading parts of this series weekly! Part 2 coming 8/21/2021! See you then π