0% found this document useful (0 votes)
6 views

Le1 Language Basics Ending - Jupyter Notebook

Uploaded by

Anthony Michael
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Le1 Language Basics Ending - Jupyter Notebook

Uploaded by

Anthony Michael
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

30/05/2023, 08:32 le1_language_basics_ending - Jupyter Notebook

Introduction to Python (continued)


Naming Rules for identifiers in Python

There certain rules we must keep in mind when picking a name for our variables, functions, classes,
modules, etc we create in Python. The most important of those rules are as follows:

1. Identifiers in Python can contain numbers (0-9), alphabets (uppercase and lowercase), and underscore
(_). These are the only characters allowed.
2. Identifiers cannot start with a number
3. Identifiers should not contain only numeric characters
4. Identifiers are case-sensitive: Number_1 is not equivalent to number_1
5. Identifiers can begin with an underscore. But this is generally only a good practice for certain instances.
Identifiers that start with single underscore or double underscore bear a special meaning in the
language.
6. Using the name of a builtin function and keywords as an identifier are invalid in some cases and should
be avoided. Some reserved keywords include:

and as assert async await break

class continue def del elif else except False

finally for from global if import in

is lambda None nonlocal not or pass

raise return True try while with yield

In [1]:

# Invalid identifiers
1st_number = 20

File "/tmp/ipykernel_5422/399934000.py", line 2


1st_number = 20
^
SyntaxError: invalid syntax

In [ ]:

$fifty_dollars = 50.0 # using a character that is not a number, alphabet or unde

File "/tmp/ipykernel_9202/2745006620.py", line 1


$fifty_dollars = 50.0 # using a character that is not a number,
alphabet or underscore
^
SyntaxError: invalid syntax

localhost:8888/notebooks/Documents/Untitled Folder/ece272/le1_language_basics_ending.ipynb 1/3


30/05/2023, 08:32 le1_language_basics_ending - Jupyter Notebook

In [ ]:

# Identifiers are case-sensitive


Number1 = 50
print(number1) # Number1 != number1

--------------------------------------------------------------------
-------
NameError Traceback (most recent cal
l last)
/tmp/ipykernel_9202/316428408.py in <module>
1 # Identifiers are case-sensitive
2 Number1 = 50
----> 3 print(number1) # Number1 != number1

NameError: name 'number1' is not defined

In [ ]:

# Avoid using the name of a builtin function.


first_number = 12.5
# We can convert first number to integer by instantiating it as an int()
first_number = int(first_number)
print(first_number)
# if we define a variable with name `int`
int = 43
# Attempting the same thing throws an error:
another_decimal = 3.142
new_number = int(another_decimal) # we have altered the meaning of `int` in our py
# To fix this, restart your python kernel and comment out the line `int = 43`

12

--------------------------------------------------------------------
-------
TypeError Traceback (most recent cal
l last)
/tmp/ipykernel_9202/3912415968.py in <module>
8 # Attempting the same thing throws an error:
9 another_decimal = 3.142
---> 10 new_number = int(another_decimal)

TypeError: 'int' object is not callable

Recommendations for choosing identifiers


The Python community have a set of conventions that developers follow when choosing identifiers for
different use-cases.

1. Variables and function (discussed later in the course) should be lowercase, with words separated by
underscores as necessary to improve readability. This naming convention is referred to as snake_case.
2. It is not recommended to use single letter l (el) or letter I as identifiers because it can be unclear
which is which in certain font styles.
3. Modules names (Python files) should have short, all-lowercase names. Underscores can be used in the
module name if it improves readability. Python packages (folders containing python files) should also
be short, all-lowercase names, although the use of underscore is discouraged.

localhost:8888/notebooks/Documents/Untitled Folder/ece272/le1_language_basics_ending.ipynb 2/3


30/05/2023, 08:32 le1_language_basics_ending - Jupyter Notebook

You can read the full style guide here: PEP 8: Style Guide for Python Code (https://peps.python.org/pep-
0008)

In [ ]:

In [ ]:

In [ ]:

localhost:8888/notebooks/Documents/Untitled Folder/ece272/le1_language_basics_ending.ipynb 3/3

You might also like