Tutorials  /  Python

Python Keywords and Identifiers

Ccentron Redaktion · February 2024 ·5 min read ·Python, Tutorial

Let’s talk about Python keywords and identifiers. We recently also covered a complete tutorial on installing and setting up Python for beginners in this Python tutorial.

Python Keywords

Well simply, Python keywords are the words that are reserved. That means you can’t use them as the name of any entities like variables, classes, and functions.

So you might be thinking what are these keywords for. They are for defining the syntax and structures of Python language.

You should know there are 33 keywords in Python programming language as of writing this tutorial. Although the number can vary in course of time. Also, keywords in Python is case sensitive. So they are to be written as it is. Here is a list of all keywords in python programming.

List of All Python Keywords

Keyword Description
and Logical operator
as Alias
assert For debugging
break Break out of Python loops
class Used for defining Classes in Python
continue Keyword used to continue with the Python loop by skipping the existing
def Keyword used for defining a function
del Used for deleting objects in Python
elif Part of the if-elif-else conditional statement in Python
else Same as above
except A Python keyword used to catch exceptions
FALSE Boolean value
finally This keyword is used to run a code snippet when no exceptions occur
for Define a Python for loop
from Used when you need to import only a specific section of a module
global Specify a variable scope as global
if Used for defining an “if” condition
import Python keyword used to import modules
in Checks if specified values are present in an iterable object
is This keyword is used to test for equality.
lambda Create anonymous functions
None The None keyword represents a Null value in Python
nonlocal Declare a variable with non-local scope
not Logical operator to negate a condition
or A logical operator used when either one of the conditions needs to be true
pass This Python keyword passes and lets the function continue further
raise Raises an exception when called with the specified value
return Exits a running function and returns the value specified
TRUE Boolean value
try Part of the try…except statement
while Used for defining a Python while loop
with Creates a block to make exception handling and file operations easy
yield Ends a function and returns a generator object

Below is a simple example showing the usage of if-else in a python program.

Python
var = 1;
if(var==1):
    print("odd")
else:
    print("even")

When we run the above program, Python understands the if-else block because of fixed keywords and syntax and then does the further processing.

VM

Matching infrastructure at centron

Scripts and services eventually need an environment that keeps running: ccloud³ VMs from €3.12 per month, billed by the hour. Rent a cloud server →

What are Python Identifiers?

Python Identifier is the name we give to identify a variable, function, class, module, or other objects. That means whenever we want to give an entity a name, that’s called an identifier.

Sometimes variable and identifier are often misunderstood as the same but they are not. Well for clarity, let’s see what is a variable?

What is a Variable in Python?

A variable, as the name indicates is something whose value is changeable over time. In fact, a variable is a memory location where a value can be stored. Later we can retrieve the value to use. But for doing it we need to give a nickname to that memory location so that we can refer to it. That’s an identifier, the nickname.

Rules for Writing Identifiers

There are some rules for writing Identifiers. But first, you must know Python is case sensitive. That means Name and name are two different identifiers in Python. Here are some rules for writing Identifiers in Python.

  • Identifiers can be a combination of uppercase and lowercase letters, digits, or an underscore(_). So myVariable, variable_1, variable_for_print all are valid python identifiers.
  • An Identifier cannot start with a digit. So while variable1 is valid, 1variable is not valid.
  • We can’t use special symbols like !,#,@,%,$ etc in our Identifier.
  • Identifier can be of any length.

Though these are hard rules for writing identifiers, also there are some naming conventions which are not mandatory but rather good practices to follow.

  • Class names start with an uppercase letter. All other identifiers start with a lowercase letter.
  • Starting an identifier with a single leading underscore indicates the identifier is private.
  • If the identifier starts and ends with two underscores, then that means the identifier is language-defined special name.
  • 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 time.
  • Multiple words can be separated using an underscore, for example, this_is_a_variable.

Here’s a sample program for python variables.

Python
myVariable="hello world"
print(myVariable)
var1=1
print(var1)
var2=2
print(var2)
Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie Python
Teilen
Noch offene Fragen?

Our team will help you with your specific setup - in German or English, by people who run the platform themselves.

War dieses Tutorial hilfreich?

Your answer is stored anonymously and helps us improve our tutorials.

Kommentare

No comments yet - be the first to ask a question about this tutorial.

Sign in to comment

Comments are open to centron customers. Sign in to your account to ask a question about this tutorial.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren