[go: up one dir, main page]

0% found this document useful (0 votes)
16 views15 pages

1

The document provides an overview of Python keywords and identifiers, explaining that keywords are predefined words with special meanings that cannot be used as identifiers. It details the rules for naming identifiers and lists examples of valid and invalid identifiers. Additionally, it covers decision-making structures and loops in Python, including if statements, while loops, and for loops, with examples demonstrating their usage.

Uploaded by

Gagandeep Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
16 views15 pages

1

The document provides an overview of Python keywords and identifiers, explaining that keywords are predefined words with special meanings that cannot be used as identifiers. It details the rules for naming identifiers and lists examples of valid and invalid identifiers. Additionally, it covers decision-making structures and loops in Python, including if statements, while loops, and for loops, with examples demonstrating their usage.

Uploaded by

Gagandeep Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 15
Python Keywords and Identifiers Last Updated :13 Aug, 2024 De Every language contains words and a set of rules that would make a sentence meaningful. Similarly, in Python programming language, there are a set of predefined words, called Keywords which along with Identifiers will form meaningful sentences when used together. Python keywords cannot be used as the names of variables, functions, and classes. In this article, we will learn about Python keywords and identifiers and how to use them to perform some tasks. Keywords in Python Python Keywords are some predefined and reserved words in Python that have special meanings. Keywords are used to define the syntax of the coding. The keyword cannot be used as an identifier, function, or variable name. All the keywords in Python are written in lowercase except True and False. There are 35 keywords in Python 3.11. In Python, there is an inbuilt keyword module that provides an iskeyword) function that can be used to check whether a given string is a valid keyword or not. Furthermore, we can check the name of the keywords in Python by using the kwlist attribute of the keyword module. Rules for Keywords in Python + Python keywords cannot be used as identifiers. + All the keywords in Python should be in lowercase except True and False. List of Python Keywords Keywords Description Keywords and not elif else for while break as def lambda pass return Description This is a logical operator which returns true if both the operands are true else returns false. This is also a logical operator which returns true if anyone operand is true else returns false. This is again a logical operator it returns True if the operand is false else returns false, This is used to make a conditional statement. Elifis a condition statement used with an if statement. The elif statements executed if the previous conditions were not true. Else is used with if and elif conditional statements. The else block is executed if the given condition is not true. This is used to create a loop. This keyword is used to create a while loop, This is used to terminate the loop. This is used to create an alternative. Ithelps us to define functions. Itis used to define the anonymous function This is a null statement which means it will do nothing, It will return a value and exit the function. True False try with assert class continue det except finally from global import This is a boolean value. This is also a boolean value. It makes a try-except statement. The with keyword is used to simplify exception handling. This function is used for debugging purposes. Usually used to check the correctness of code It helps us to define a class. It continues to the next iteration of a loop Itdeletes a reference to an object: Used with exceptions, what to do when an exception occurs Finally is used with exceptions, a block of code that will be executed no matter if there is an exception or not. it is used to import specific parts of any module. This declares a global variable. This is used to import a module, in It's used to check whether a value is presentiin a list, range, tuple, etc. is This is used to check if the two variables are equal or not. This is a special constant used to denote a null value or avoid. It's important to remember, 0, ens any empty container(eg empty list) do not compute to None nontocal. It's declared a non-Local variable. raise This raises an exception. yield It ends a function and returns a generator. async Itis used to create asynchronous coroutine. await It releases the flow of control back to the event loop. Identifiers in Python Identifier is a user-defined name given to a variable, function, class, module, etc. The identifier is a combination of character digits and an underscore. They are case-sensitive i.e., num’ and ‘Num’ and ‘NUM are three different identifiers in python. It is a good programming practice to give meaningful names to identifiers to make the code understandable We can also use the Python string isidentifier() method to check whether a string is a valid identifier or not. Rules for Naming Python Identifiers * It cannot be a reserved python keyword. * It should not contain white space. + Itcan be a combination of A-Z, a-z, 0-9, or underscore. © It should start with an alphabet character or an underscore ( _ ). * It should not contain any special character other than an underscore ( _ ) Examples of Python Identifiers Valid identifiers: © vard * _varl © _1var © vart Invalid Identifiers « Ivart © lvar = Lvar © vart1 © varl Python - Decision Making < Previous Python's decision making functionality is in its keywords — if..elif...else. The if keyword requires a boolean expression, followed by colon (:) symbol. The colon (:) symbol starts an indented block. The statements with the same level of indentation are executed if the boolean expression in if statement is True. If the expression is not True (False), the interpreter bypasses the indented block and proceeds to execute statements at earlier indentation level. Decision structures evaluate multiple expressions which produce TRUE or FALSE as outcome. You need to determine which action to take and which statements to execute if outcome is TRUE or FALSE otherwise. Following is the general form of a typical decision making structure found in most of the programming languages — rrr If condition is true if condition is false Pe TET} feed Python programming language assumes any non-zero and non- null values as TRUE, and if it is either zero or null, then it is assumed as FALSE value. Types of Decision Making Statements in Python Python programming language provides following types of decision making statements. Click the following links to check their detail. Sr.No. Statement & Description if statements 1 An if statement consists of a boolean expression followed by one or more statements. if...else statements 2 An if statement can be followed by an optional else statement, which executes when the boolean if...else statements An if statement can be followed by an optional else statement, which executes when the boolean expression is FALSE. nested if statements 3 You can use one if or else if statement inside another if or else if statement(s). Let us go through each decision making briefly — Single Statement Suites If the suite of an af clause consists only of a single line, it may go on the same line as the header statement. Example Here is an example of a one-line if clause — ore ne Se Eg When the above code is executed, it produces the following result Value of expression is 100 Good bye! if...else statement In this decision making statement, if the if condition is true, then the statements within this block are executed, otherwise, the else block is executed. The program will choose which block of code to execute based on whether the condition in the if statement is true or false. Example The following example shows the use of if...else statement. ee) )3 int ("Value of var is equal to 10") ("Value of var is not equal to 100") On running the above code, it will show the following output — Value of var is equal to 100 Nested if statements A nested if is another decision making statement in which one if statement resides inside another. It allows us to check multiple conditions sequentially. Nested if statements A nested if is another decision making statement in which one if statement resides inside another. It allows us to check multiple conditions sequentially. Example In this example, we will see the use of nested-if statement. ("The given number int("The given number is negative") On executing the above code, it will display the below output — The number is equal to 100 The number is even Loops in Python - For, While and Nested Loops ° Oe} Last Updated : 20 Jun, 2024 Python programming language provides two types of Python loopshecking time. In this article, we will look at Python loops and understand their working with the help of examp ~ For loop and While loop to handle Looping requirements. Loops in Python provides three ways for executing the loops. While all the ways provide similar basic functionality, they differ in their syntax and condition-checking time. In this article, we will look at Python loops and understand their working with the help of examples. While Loop in Python In Python, a while loop is used to execute a block of statements repeatedly until a given condition is satisfied. When the condition becomes false, the line immediately after the loop in the program is executed, Python While Loop Syntax: while expression: statement (s) All the statements indented by the same number of character spaces after a programming construct are considered to be part of a single block of code. Python uses indentation as its method of grouping statements. Let's learn how to use a while Loop in Python with Examples: Example of Python While Loop Let's see a simple example of a while loop in Python. The given Python code uses a ‘uhile’ loop to print “Hello Geek" three times by incrementing a variable called ‘count’ from 1 to 3. Example of Python While Loop Let's see a simple example of a while loop in Python. The given Python code uses a ‘while’ loop to print “Hello Geek” three times by incrementing a variable called ‘count' from 1 to 3, Python count = 0 while (count < 3): count - count + 1 print ("Hello Geek") D Output Hello Geek Hello Geek Hello Geek For Loop in Python For loops are used for sequential traversal. For example: traversing a list or string or array-etc. In Python, there is “for in” loop which is similar to foreach loop in other languages. Let us [eam how to use for loops in Python for sequential traversals with examples, For Loop Syntax: for iterator_var in sequence: statements(s) Itcan be used to iterate over a range and iterators. Example: The code uses a Python for loop that iterates over the values from 0 to 3 (not including 4), as specified by the range(o, n) construct. It will print the values of ‘i* in each iteration of the loop. Python o n=4 for i in range(, n): o print(i) > & Output Uno Nested Loops in Python Python programming language allows to use one loop inside another toop which is called nested Loop. Following section shows few examples to illustrate the concept. Nested Loops Syntax: for iterator_var in sequence: for iterator_var in sequence: statements(s) statements(s) The syntax for a nested while loop statement in the Python programming language is as follows: while expression uhile expression: statement (s) statement(s) A final note on loop nesting is that we can put any type of loop inside of any other type of Loops in Python. For example, a for loop can be inside a while loop or vice versa. Example: This Python code uses nested ‘for' loops to create a triangular pattern of numbers. It iterates from 1 to 4 and, in each iteration, prints the current number multiple times based on the iteration number. The result is a pyramid-like pattern of numbers, Python ‘uture__ import print_function for i in range(1, 5): for j in range(i) print(i, end=" *) > print() Output

You might also like