Thanks to visit codestin.com
Credit goes to www.scribd.com

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

2 Identifiers and Variables

The document defines identifiers, variables, and data types in Python. It states that an identifier is a name given to entities like variables and functions. It then describes the rules for writing valid identifiers in Python, including that they can include letters, numbers, and underscores but cannot start with a number or include special characters. The document also discusses variables, noting that a variable stores a value in memory and follows the same naming rules as identifiers. It explains single and multiple assignment of values to variables. Finally, it provides an overview of different data types in Python like strings, integers, lists, and dictionaries.

Uploaded by

Subbu Suni
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)
155 views16 pages

2 Identifiers and Variables

The document defines identifiers, variables, and data types in Python. It states that an identifier is a name given to entities like variables and functions. It then describes the rules for writing valid identifiers in Python, including that they can include letters, numbers, and underscores but cannot start with a number or include special characters. The document also discusses variables, noting that a variable stores a value in memory and follows the same naming rules as identifiers. It explains single and multiple assignment of values to variables. Finally, it provides an overview of different data types in Python like strings, integers, lists, and dictionaries.

Uploaded by

Subbu Suni
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

IDENTIFIERS ,

VA R I A B L E S &
D ATA T Y P E S

DEPARTMENT OF MECHANICAL ENGINEERING


DEFINITION

 An identifier is nothing but a name given to entities like class,

functions, variables, etc.

OR

 If you assign some name to a programmable entity in Python, then it is

nothing but technically called an identifier.

 It helps to differentiate one entity from another. Python Identifiers are

user-defined names represent a variable, function, class, module or

any other object.

D E PA R T M E N T O F M E C H A N I C A L E N G I N E E R I N G
RULES FOR WRITING IDENTIFIERS
 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 _.

For example Names like

– total,

– class_tea and

– Father_name

 An identifier cannot start with a digit

For example

1Name is invalid.

 We cannot use special symbols like !, @, #, $, % etc. in our identifier.

 We cannot use reserved words as a identifiers

D E PA R T M E N T O F M E C H A N I C A L E N G I N E E R I N G
WHICH THE FOLLOWING ARE VALID PYTHON IDENTIFIERS

 123total

 total123

 java2share

 ca$h

 _abc_abc

 def

 if

D E PA R T M E N T O F M E C H A N I C A L E N G I N E E R I N G
VARIABLES:
 Variable is a name which is used to store the value in a memory location.

 Variable also known as identifier and used to hold value.

 Variable names can be defined using the rules of defining identifier using

group of both letters and digits, but they must begin with a letter or an

underscore.

 Assigning value to variable

1. Single Assignment

2. Multiple Assignment

D E PA R T M E N T O F M E C H A N I C A L E N G I N E E R I N G
SINGLE ASSIGNMENT
 Input

– A=10

– B=“mike”

– C=20000.67

 Output:

– 10

– Mike

– 20000.67

D E PA R T M E N T O F M E C H A N I C A L E N G I N E E R I N G
MULTIPLE ASSIGNMENT
 We can assign a value to multiple variable in a single line or statement.

 We can use multiple assignments in two ways :

1. Either by assigning a single value to multiple variables in a single


line/statement or

2. Assigning multiple values to multiple variables.

D E PA R T M E N T O F M E C H A N I C A L E N G I N E E R I N G
ASSIGNING SINGLE VALUE TO MULTIPLE VARIABLES

 Input

– A=B=C=50

 Output:

– 50

– 50

– 50

D E PA R T M E N T O F M E C H A N I C A L E N G I N E E R I N G
ASSIGNING MULTIPLE VALUES TO MULTIPLE VARIABLES

 Input

– A,B,C=5,10,15

 Output:

– 5

– 10

– 15

D E PA R T M E N T O F M E C H A N I C A L E N G I N E E R I N G
TYPES OF VARIABLES

Global variables

 Variables that are created outside of a function are known as global


variables.

 Global variables can be used by everyone, both inside of functions and


outside.
• x = "awesome"

def myfunc():
  x = "fantastic"
  print("Python is " + x)

myfunc()

print("Python is " + x)

D E PA R T M E N T O F M E C H A N I C A L E N G I N E E R I N G
• Local variables

 Variables that are created INSIDE of a function are known as LOCAL

variables.
• def myfunc():
  global x
  x = "fantastic"

myfunc()

print("Python is " + x)

D E PA R T M E N T O F M E C H A N I C A L E N G I N E E R I N G
PYTHON COMMENTS
 Comments are very important while writing a program.

 Python Interpreter ignores comment.

 Python supports two types of comments:

Single line comment:


 In case user wants to specify a single line comment, then comment must
start with (#)

Multi line comment:


 If we have comments that extend multiple lines, one way of doing it is to use
hash (#) in the beginning of each line.

 Multi lined comment can be given inside triple quotes.

D E PA R T M E N T O F M E C H A N I C A L E N G I N E E R I N G
DATA TYPES
 Data Type represent the type of data present inside a variable

 Python has the following data types built-in by default, in these


categories:

Text Type: str


Numeric Types: int, float, complex
Sequence Types: list, tuple, range
Mapping Type: dict
Set Types: set, frozenset
Boolean Type: bool
Binary Types: bytes, bytearray, memoryview

D E PA R T M E N T O F M E C H A N I C A L E N G I N E E R I N G
SETTING THE DATA TYPE

Data Type Sample Example


str x = "Hello World" x = str("Hello World")
int x = 20 x = int(20)
float x = 20.5 x = float(20.5)
complex x = 1j x = complex(1j)
list x = ["apple", "banana", "cherry"] x = list(("apple", "banana", "cherry"))
tuple x = ("apple", "banana", "cherry") x = tuple(("apple", "banana", "cherry"))
range x = range(6) x = range(6)
dict x = {"name" : "John", "age" : 36} x = dict(name="John", age=36)
set x = {"apple", "banana", "cherry"} x = set(("apple", "banana", "cherry"))

D E PA R T M E N T O F M E C H A N I C A L E N G I N E E R I N G
SETTING THE DATA TYPE

Data Type Sample Example


x = frozenset(("apple",
frozenset x = frozenset({"apple", "banana", "cherry"}) "banana", "cherry"))

x = bool(5)
bool x = True
x = bytes(5)
bytes x = b"Hello"
x = bytearray(5)
bytearray x = bytearray(5)
x = memoryview(bytes(5))
memoryview x = memoryview(bytes(5))

D E PA R T M E N T O F M E C H A N I C A L E N G I N E E R I N G
ESCAPE CHARACTERS
• New line
• Horizontal tab
• Back space
• Vertical tab
• Single quote
• Double quote

D E PA R T M E N T O F M E C H A N I C A L E N G I N E E R I N G

You might also like