13 Lecture Note Unit 1 Variables Datatypes Type Id Pcps 2023
13 Lecture Note Unit 1 Variables Datatypes Type Id Pcps 2023
Lecture Notes
Python for Computational Problem Solving
UE23CS151A
Lecture #13
Variables, Data types and id
By,
Prof. Sindhu R Pai,
Anchor, PCPS - 2023
Assistant Professor
Dept. of CSE, PESU
Verified by,
PCPS Team - 2023
Many Thanks to
Dr. Shylaja S S (Director, CCBD and CDSAML Research Centers, Former
Chairperson, CSE, PES University)
Prof. Chitra G M (Asst. Prof, Dept. of CSE, PCPS Anchor – 2022)
Aug – Dec, 2023
Identifiers:
A sequence of one or more characters used to provide a name for a given program
element. Python is case sensitive. Example: Line is different from line. Few rules to be
followed are here:
• Identifiers may contain letters and digits but cannot begin with a digit.
• The underscore character (_) is also allowed to aid in the readability of long identifier
names. However, it should not be the first character, as identifiers beginning with an
underscore have special meaning in Python.
Keywords:
An identifier that has predefined meaning in a programming language. Therefore,
keywords cannot be used as “regular” identifiers. Doing so will result in a syntax error.
How do you list the set of keywords in python?
Note: Press q to come out of help page and then enter exit() / quit()
Variables:
A variable is a name (identifier) that is associated with a value. A variable is created
the moment you first assign a value to it. It can be assigned different values during a
program execution. Hence the name variable.
Note: Every variable in Python is a reference (a pointer) to an object and not the actual
value itself. For example, the assignment statement just adds a new reference to the right-
hand side.
Program 1:
a = 10
k = 10
print(a)
print(k)
Output: 10
10
Program 2:
a = 10
k =a
print(a)
print(k)
Above diagram and output remains the same.
Point to think: If you change the value of k, will the value of a be changed? – Nooo!
Observation: In 1 and 2, Variables a and k are both associated with the same literal value
10 in memory. One way to prove this is by the use of built-in function id().
id() : Id function produces a unique number identifying a specific value (object) in memory.
Program 5: usage of variable sep and end along with the keyword parameter for print
Literals:
In arithmetic, we talk about values which do not change. Even in programming, we
talk about entities which do not change. They are considered as they are. They are said to
be constants or literals. A literal is a sequence of one or more characters that stands for itself.
Literals can be categorized into: Numeric literals and String Literals
Numeric Literals:
• A literal containing only the digits 0–9, an optional sign character (+ or -), and a
possible decimal point.
• If a numeric literal contains a decimal point, then it denotes a floating-point value ,
or “ float ” (e.g., 10.24); otherwise, it denotes an integer value (e.g., 10).
• Commas are never used in numeric literal.
• Complex numbers have a real and imaginary part.
• There is no limit to the size of an integer that can be represented in Python
Data Types:
Every variable is associated with a type based on the value assigned to it.
Significance of data types are listed below.
• Range of values allowed in a variable.
• Memory allocation for a variable
• Allowed set of operations on the data
type() in python: A built in function, which returns the type of the given object.
Reference types:
Structured or reference types having more than one value.
- END -