Python Keywords and Identifiers
Python Keywords and Identifiers
There are 33 keywords in Python 3.7. This number can vary slightly in the course
of time.
as elif if or yield
Keywords in Python
Python Identifiers
An identifier is a name given to entities like class, functions, variables, etc. It helps
to differentiate one entity from another.
Rules for writing identifiers
1. Identifiers can be a combination of letters in lowercase (a to z) or
uppercase (A to Z) or digits (0 to 9) or an underscore _. Names
like myClass, var_1 and print_this_to_screen, all are valid example.
2. An identifier cannot start with a digit. 1variable is invalid, but variable1 is
perfectly fine.
3. Keywords cannot be used as identifiers.
a.
b. >>> global = 1
c. File "<interactive input>", line 1
d. global = 1
e. ^
f. SyntaxError: invalid syntax
4. We cannot use special symbols like !, @, #, $, % etc. in our identifier.
a.
b. >>> a@ = 0
c. File "<interactive input>", line 1
d. a@ = 0
e. ^
f. SyntaxError: invalid syntax
5. Identifier can be of any length.
Things to Remember
Python is a case-sensitive language. This means, Variable and variable are not the
same. Always name identifiers that make sense.
While, c = 10 is valid. Writing count = 10 would make more sense and it would be
easier to figure out what it does even when you look at your code after a long gap.
Multiple words can be separated using an underscore, this_is_a_long_variable.
1. a=1+2+3+\
2. 4+5+6+\
3. 7+8+9
1. a = (1 + 2 + 3 +
2. 4+5+6+
3. 7 + 8 + 9)
1. colors = ['red',
2. 'blue',
3. 'green']
We could also put multiple statements in a single line using semicolons, as follows
1. a = 1; b = 2; c = 3
Python Indentation
Most of the programming languages like C, C++, Java use braces { } to define a
block of code. Python uses indentation.
A code block (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 must be
consistent throughout that block.
Generally four whitespaces are used for indentation and is preferred over tabs.
Here is an example.
script.py
IPython Shell
1
2
3
4
for i in range(1,11):
print(i)
if i == 5:
break
Python Indentation
Generally four whitespaces are used for indentation and is preferred over
tabs. Here is an example.
script.py
IPython Shell
1
2
3
4
for i in range(1,11):
print(i)
if i == 5:
break
Run
Powered by DataCamp
The enforcement of indentation in Python makes the code look neat and
clean. This results into Python programs that look similar and consistent.
1. if True:
2. print('Hello')
3. a = 5
and
1. if True: print('Hello'); a = 5
both are valid and do the same thing. But the former style is clearer.
Incorrect indentation will result into IndentationError.
Python Comments
1. #This is a comment
2. #print out Hello
3. print('Hello')
Multi-line comments
These triple quotes are generally used for multi-line strings. But they can
be used as multi-line comment as well. Unless they are not docstrings, they
do not generate any extra code.
1. """This is also a
2. perfect example of
3. multi-line comments"""
Docstring in Python
Docstring is short for documentation string.
script.py
IPython Shell
1
2
3
def double(num):
"""Function to double the value"""
return 2*num
Run
Powered by DataCamp
Generally four whitespaces are used for indentation and is preferred over tabs.
Here is an example.
script.py
IPython Shell
1
2
3
4
for i in range(1,11):
print(i)
if i == 5:
break
Run
Powered by DataCamp
Python Variables
A variable is a named location used to store data in the memory. It is
helpful to think of variables as a container that holds data which can be
changed later throughout programming. For example,
1. number = 10
Here, we have created a named number. We have assigned value 10 to the
variable.
You can think variable as a bag to store books in it and those books can be
replaced at any time.
1. number = 10
2. number = 1.1
Initially, the value of number was 10. Later it's changed to 1.1.
1
2
website = "apple.com"
print(website)
Run
Powered by DataCamp
apple.com
In the above program, we assigned a value apple.com to the
variable website. Then we print the value assigned
to website i.e. apple.com
Note : Python is a type inferred language; it can automatically
know apple.com is a string and declare website as a string.
1
2
3
4
5
6
7
website = "apple.com"
print(website)
# assigning a new variable to website
website = "programiz.com"
print(website)
Run
Powered by DataCamp
apple.com
programiz.com
In the above program, we have assigned apple.com to
the website variable initially. Then, it's value is changed
to programiz.com.
1
2
3
4
5
a, b, c = 5, 3.2, "Hello"
print (a)
print (b)
print (c)
Run
Powered by DataCamp
script.py
IPython Shell
1
2
3
4
5
x = y = z = "same"
print (x)
print (y)
print (z)
Run
Powered by DataCamp
The second program assigns the same string to all the three
variables x, y and z.
Constants
Non technically, you can think of constant as a bag to store some books
and those books cannot be replaced once placed inside the bag.
Create a constant.py
1. PI = 3.14
2. GRAVITY = 9.8
Create a main.py
1. import constant
2.
3. print(constant.PI)
4. print(constant.GRAVITY)
4. myAge
myAddress
7. G
8. MASS
TEMP
14. MACRO_CASE
15. camelCase
CapWords
Literals
Numeric Literals
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
a = 0b1010 #Binary Literals
b = 100 #Decimal Literal
c = 0o310 #Octal Literal
d = 0x12c #Hexadecimal Literal
#Float Literal
float_1 = 10.5
float_2 = 1.5e2
#Complex Literal
x = 3.14j
print(a, b, c, d)
print(float_1, float_2)
print(x, x.imag, x.real)
Run
Powered by DataCamp
String literals
print(fruits)
print(numbers)
print(alphabets)
print(vowels)
strings = "This is Python"
char = "C"
multiline_str = """This is a multiline string with more than
one line code."""
unicode = u"\u00dcnic\u00f6de"
raw_str = r"raw \n string"
print(strings)
print(char)
print(multiline_str)
print(unicode)
print(raw_str)
Run
Powered by DataCamp
When you run the program, the output will be:
This is Python
C
This is a multiline string with more than one line
code.
Ünicöde
raw \n string
In the above program, This is Python is a string literal and C is a
character literal. The value with triple-quote """ assigned in
the multiline_str is multi-line string literal.
The u"\u00dcnic\u00f6de" is a unicode literal which supports characters
other than English and r"raw \n string" is a raw string literal.
Boolean literals
A Boolean literal can have any of the two values: True or False.
Example 8: How to use boolean literals in Python?
script.py
IPython Shell
1
2
3
4
5
6
7
8
9
x = (1 == True)
y = (1 == False)
a = True + 4
b = False + 10
print("x is", x)
print("y is", y)
print("a:", a)
print("b:", b)
Run
Powered by DataCamp
x is True
y is False
a: 5
b: 10
In the above program, we use boolean literal True and False. In
Python, True represents the value as 1 and False as 0. The value
of x is True because 1 is equal to True. And, the value
of y is False because 1 is not equal to False.
Similarly, we can use the True and False in numeric expressions as the
value. The value of a is 5 because we add True which has value
of 1 with 4. Similarly, b is 10 because we add the False having value
of 0 with 10.
When you run the program, the output will be:
Available
None
In the above program, we define a menu function. Inside menu, when we set
parameter as drink then, it displays Available. And, when the parameter
is food, it displays None.
Literal Collections
There are four different literal collections List literals, Tuple literals, Dict
literals, and Set literals.
1
2
3
4
5
6
7
8
9
fruits = ["apple", "mango", "orange"] #list
numbers = (1, 2, 3) #tuple
alphabets = {'a':'apple', 'b':'ball', 'c':'cat'} #dictionary
vowels = {'a', 'e', 'i' , 'o', 'u'} #set
print(fruits)
print(numbers)
print(alphabets)
print(vowels)
Run
Powered by DataCamp