0% found this document useful (0 votes)
17 views16 pages

Lecture 2

The document provides an overview of Python identifiers, including rules for defining them, naming conventions, and reserved words. It explains that identifiers must start with a letter or underscore, cannot contain special characters, and are case-sensitive. Additionally, it discusses variable types and how to assign values to variables in Python, along with examples and exercises for practice.

Uploaded by

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

Lecture 2

The document provides an overview of Python identifiers, including rules for defining them, naming conventions, and reserved words. It explains that identifiers must start with a letter or underscore, cannot contain special characters, and are case-sensitive. Additionally, it discusses variable types and how to assign values to variables in Python, along with examples and exercises for practice.

Uploaded by

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

Learn Python

Lecture 2
By Dr. Vibha Tiwari
Identifiers

• A name in Python program is called identifier.


• It can be class name or function name or module
name or variable name.

example x = 10
Rules to define identifiers in
Python:
• The only allowed characters in Python are

• alphabet symbols(either lower case or upper case)

• digits(0 to 9)

• underscore symbol(_)
Python Identifiers:
• An identifier starts with a letter A to Z or a to z or
an underscore (_) followed by zero or more
letters, underscores, and digits (0 to 9).
• Python does not allow punctuation characters
such as @, $, and % within identifiers.
Identifiers
• By mistake if we are using any other symbol like $ then we will get
syntax error.

• cash = 10 √

• ca$h =20 x
Identifiers
• Identifier should not starts with digit
• 123total x
• total123 √
• Python is a case sensitive programming language. Thus Manpower
and manpower are two different identifiers in Python.
• total=10
• TOTAL=999
• print(total) #10
• print(TOTAL) #999
Identifiers
• We cannot use reserved words as identifiers
Eg: def=10 r
• There is no length limit for Python identifiers. But not
recommended to use too lengthy identifiers.
Python Identifiers (cont’d)
• Here are following identifier naming convention for Python:
• Class names start with an uppercase letter and all other identifiers
with a lowercase letter.
• Starting an identifier with a single leading underscore indicates by
convention that the identifier is meant to be private.
• Starting an identifier with two leading underscores indicates a
strongly private identifier.
• If the identifier also ends with two trailing underscores, the identifier
is a language-defined special name, which is also known as magic
methods.
Eg: __add__
Reserved Words

• In Python some words are reserved to represent some meaning or


functionality. Such type of words are called Reserved words.
• There are 33 reserved words available in Python.
Reserved Words:
Keywords contain lowercase letters only.
and exec not
assert finally or
break for pass
class from print
continue global raise
def if return
del import try
elif in while
else is with
except lambda yield
• All Reserved words in Python contain only alphabet symbols.
• Except the following 3 reserved words, all contain only lower case
alphabet symbols.
• True
• False
• None
• import keyword
• keyword.kwlist

• ['False', 'None', 'True', 'and', 'as', 'assert', 'break', 'class', 'continue',
'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if',
'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise',
'return', 'try', 'while', 'with', 'yield']
Python - Variable Types
• Variables are nothing but reserved memory locations to store values.
This means that when you create a variable you reserve some space
in memory.
• Based on the data type of a variable, the interpreter allocates
memory and decides what can be stored in the reserved memory.
Therefore, by assigning different data types to variables, you can store
integers, decimals, or characters in these variables.
Assigning Values to Variables:
• Python variables do not have to be explicitly declared to reserve
memory space. The declaration happens automatically when you
assign a value to a variable. The equal sign (=) is used to assign values
to variables.
counter = 100 # An integer assignment
miles = 1000.0 # A floating point
name = "John" # A string
print counter
print miles
print name
Multiple Assignment:
• You can also assign a single value to several variables simultaneously.
For example:
a = b = c = 1
a, b, c = 1, 2, "john"
Exercise
• Write
1. Python Program to Find the Square Root without using Square-root
function.
2. Python Program to Calculate the Area of a Triangle where its three
sides a, b, c are given. s=(a+b+c)/2 Area=square root of s(s-a)(s-b)(s-c)
3. Python Program to Solve Quadratic Equation
4. Python Program to Swap Two Variables
5. Python Program to Convert Kilometers to Miles
6. Python Program to Convert Celsius to Fahrenheit

You might also like