0% found this document useful (0 votes)
2 views

identifiers data types

The document outlines the rules for creating identifiers, the nature of variables, and the significance of keywords in Python. It explains that identifiers must follow specific naming conventions, variables store data values and can be mutable or immutable, and keywords are reserved words that cannot be used as identifiers. Additionally, it covers basic data types in Python, including numbers, strings, booleans, and the process of type conversion.

Uploaded by

Remya Gopinadh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

identifiers data types

The document outlines the rules for creating identifiers, the nature of variables, and the significance of keywords in Python. It explains that identifiers must follow specific naming conventions, variables store data values and can be mutable or immutable, and keywords are reserved words that cannot be used as identifiers. Additionally, it covers basic data types in Python, including numbers, strings, booleans, and the process of type conversion.

Uploaded by

Remya Gopinadh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Identifiers

Identifiers are names used to identify variables, functions, classes, modules, and other objects in
Python.

Rules for framing identifiers

1.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 _.

Names like myClass, var_1 and print_this_to_screen, all are valid example.

2.An identifier cannot start with a digit.

1variable is invalid, but variable1 is a valid name.

3.Keywords cannot be used as identifiers.

Eg: global = 1 is invalid

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

a@=0 is invalid

5.An identifier can be of any length.

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.

 Multiple words can be separated using an underscore, like this_is_a_long_variable.

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'>

use dir(object) to know the functions associated with the object

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:

 Variables are created when they are first assigned a value.

 Variables type is determined by the value which is assigned.

 Variables must be assigned a value before using them in expression,

 Variables refer to an object and are never declared ahead of time.

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.

In Python, keywords are case sensitive.

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

The following Python code will list all keywords

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.

 Variables: Containers for storing data values.

 Keywords: Reserved words with special meanings in Python.

Basic Data Types in Python and Type Conversions

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.

Integers can be of any length, it is only limited by the memory available.


A floating-point number is accurate up to 15 decimal places. Integer and floating points are
separated by decimal points. 1 is an integer, 1.0 is a floating-point number.

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.1234567890123456789 # note that b is truncated to 15 decimal places

b 0.12345678901234568

You can also delete the reference to a number object by using the del statement

>>>del a

Here are some examples of numbers.

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 """.

>>>s = "This is a string"

>>>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"

>>>S[2:5] # extract characters 2-4

>>>tho

>>>S[2]='d' # this will generate error because string is immutable

>>>S*2

PythonPython

Note: More about strings and string functions will be discussed later.

Boolean Data Type

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):

File "<input>", line 1, in <module>

NameError: name 'true' is not defined

None Type

None is used to represent the absence of a value.

Eg x=None

Conversion between data types

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

The round() function rounds a float to the nearest int

>>>round(10.6)

11
Conversion to and from string must contain compatible values.

>>> float('2.5')

2.5

>>> str(25)

'25'

>>> int('1p')

Traceback (most recent call last):

File "<string>", line 1, in <module>",

ValueError: invalid literal for int() with base 10: '1p'

Converting integer to character

>>>chr(65)
'A'

Converting single character to integer value

>>ord('A')

65

Convert integer to octal string

>>>oct(12)

0o14

Convert integer to hexadecimal string

>>>hex(12)

0xc

Convert integer to binary

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

Type coercion ( old version 2.x)

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.

You might also like