identifiers data types
identifiers data types
Identifiers are names used to identify variables, functions, classes, modules, and other objects in
Python.
Names like myClass, var_1 and print_this_to_screen, all are valid example.
a@=0 is invalid
Things to Remember
Python is a case-sensitive language. This means, variable X and x are not the same.
Always give the identifiers a name that makes sense. While c = 10 is a valid name, writing
count=10 would make more sense, and it would be easier to figure out what it represents
when you look at your code after a long gap.
Variables
A variable can be used to store a certain value or object. In Python, all numbers (and everything else,
including functions) are objects. A variable is created through assignment. Their type is assigned
dynamically.
Eg:
x='binu'
y=23
z=24.5
l=399379379379387983793773973977000102
use type(object) to know the type of the variable object
Eg: type(x) type(y) type(z) type(l)
<type 'str'> <type 'int'> <type 'float'> <type 'long'>
When we create a program, we often like to store values so that it can be used later. We use objects
to capture data, which then can be manipulated by computer to provide information. By now we
know that object/ variable is a name which refers to a value. It has a type and also store some value.
Variable can be mutable or immutable. A mutable variable is one whose value may change in place,
whereas in an immutable variable change of value will not happen in place.
Things to Remember:
Keywords
Keywords are reserved words that have a special meaning in Python. They are part of the syntax and
cannot be used as identifiers.
We cannot use a keyword as a variable name, function name or any other identifier. They are used to
define the syntax and structure of the Python language.
There are 36 keywords in Python 3.12. This number can vary slightly over the course of time.
All the keywords except True, False and None are in lowercase and they must be written as they are.
The list of all the keywords is given below.
False
None
True
and
as
assert
async
await
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
import keyword
print(keyword.kwlist)
['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', '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']
Summary
Identifiers: Names for variables, functions, etc. Must follow specific rules.
Every value in Python has a datatype. Since everything is an object in Python programming,
data types are actually classes and variables are instance (object) of these classes.
There are various data types in Python. Some of the basic types are listed below.
Python Numbers
Integers, floating point numbers and complex numbers fall under Python numbers category.
They are defined as int, float and complex classes in Python.
We can use the type() function to know which class a variable or a value belongs to.
Similarly, the isinstance() function is used to check if an object belongs to a particular class.
Complex numbers are written in the form, x + yj, where x is the real part and y is the
imaginary part. Here are some examples.
>>>a=5
>>>b=4.5
>>>c=3+2j
>>>type(a)
<class 'int'>
>>>type(b)
<class 'float'>
>>>type(c)
<class 'complex'>
>>>isinstance(a,int)
True
b 0.12345678901234568
You can also delete the reference to a number object by using the del statement
>>>del a
in float co
t mpl
ex
1 0.0 3.1
0 4j
1 15.2 45.j
0 0
0
- -21.9 9.3
7 22e
8 -36j
6
0 32.3 .87
8 +e18 6j
0
- -90. -.65
0 45+
4 0J
9
0
- - 3e+
0 32.5 26J
× 4e10
2 0
6
0
0 70.2- 4.5
× E12 3e-
6 7j
9
Binary,Hexa decimal and octal values can also be assigned to variable with prefix 0b,0x and
0o
>>>x=0b1100
>>>x
12
>>>x=0xc
>>>x
12
>>>x=0o12
>>>x
10
Python Strings
String is sequence of Unicode characters. We can use single quotes or double quotes to
represent strings. Multi-line strings can be denoted using triple quotes, ''' or """.
>>>print(s)
This is a string
s = '''A multiline
string'''
>>>print(s)
A multiline
string
slicing operator [ ] can be used with strings to extract characters from Strings, however they
are immutable ie; we cannot change the characters.
>>>S="Python"
>>>tho
>>>S*2
PythonPython
Note: More about strings and string functions will be discussed later.
The boolean data type is either True or False. In Python, boolean variables are defined by
the True and False keywords
.>>> a = True
>>> type(a)
<class 'bool'>
>>> b = False
>>> type(b)
<class 'bool'>
The output <class 'bool'> indicates the variable is a boolean data type.
Note the keywords True and False must have an Upper Case first letter. Using a lowercase
true returns an error.
>>> c = true
Traceback (most recent call last):
None Type
Eg x=None
We can convert between different data types by using different type conversion functions
like int(), float(), str(), etc.
>>> float(5)
5.0
Conversion from float to int will truncate the value (make it closer to zero).
>>> int(10.6)
10
>>> int(-10.6)
-10
>>>round(10.6)
11
Conversion to and from string must contain compatible values.
>>> float('2.5')
2.5
>>> str(25)
'25'
>>> int('1p')
>>>chr(65)
'A'
>>ord('A')
65
>>>oct(12)
0o14
>>>hex(12)
0xc
>>>bin(12)
0b1100
Integers and floating point numbers can be converted to the boolean data type using
Python's bool() function. An int, float or complex number set to zero returns False. An
integer, float or complex number set to any other number, positive or negative, returns True.
>>> zero_int = 0
>>> bool(zero_int)
False
>>> pos_int = 1
>>> bool(pos_int)
True
>>> neg_flt = -5.1
>>> bool(neg_flt)
True
Now that we can convert between types, we have another way to deal with integer division.
Suppose we want to calculate the fraction of an hour that has elapsed. The most obvious
expression,
minute / 60,
does integer arithmetic, so the result is always 0, even at 59 minutes past the hour.
One solution is to convert minute to floating-point and do floating-point division:
>>> minute = 59
>>> float(minute) / 60
0.983333333333
Alternatively, we can take advantage of the rules for automatic type conversion,which is
called type coercion. For the mathematical operators, if either operand is a float, the other is
automatically converted to a float:
>>> minute = 59
>>> minute / 60.0
0.983333333333
By making the denominator a float, we force Python to do floating-point division.
Summary
Python provides a rich set of basic data types that allow for versatile and powerful data
manipulation. Understanding these fundamental types is crucial for writing effective and
efficient Python code.