0% found this document useful (0 votes)
14 views11 pages

Identiyfier, Literals and Type Casting

Uploaded by

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

Identiyfier, Literals and Type Casting

Uploaded by

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

Identiyfier, Literals and Type Casting

Literals

• A literal is a fixed value that is directly written in the code. It


represents constant values that do not change during program
execution.

• age = 25 # 25 is a numeric literal


• name = "Pawan" # "Pawan" is a string literal
• is_valid = True # True is a boolean literal
Identifier

• An identifier is a name used to identify variables, functions, classes,


modules, or other objects. It helps you refer to objects by name.
• In Python, there is no fixed limit on the length of an identifier.

• my_var = 25 # 'my_var' is an identifier


• def my_function(): # 'my_function' is an identifier
pass
Key Differences:
Aspect Literals Identifiers

Definition Fixed values used directly in the Names used to represent


code. variables, functions, etc.

Types Numeric, String, Boolean, Variables, function names, class


Special (None), etc. names, etc.
Mutability Immutable (values cannot be Mutable (values of variables
changed). can change).
Usage Directly represent data in the Refer to objects or data stored
code. in memory.
Example my_var, function_name,
42, "Hello", True, None
PersonClass
Typecasting in python

• The conversion of one data type into the other data


type is known as type casting in python or type
conversion in python.

• Python supports a wide variety of functions or methods


like: int(), float(), str(), ord(), hex(), oct(),
tuple(), set(), list(), dict(), etc. for the type
casting in python.
Two Types of Typecasting:

• 1. Explicit Conversion (Explicit type casting in


python)

• 2. Implicit Conversion (Implicit type casting in


python).
1-Explicit typecasting:

• The conversion of one data type into another data type, done
via developer or programmer's intervention or manually as
per the requirement, is known as explicit type conversion

• It can be achieved with the help of Python’s built-in type


conversion functions such as int(), float(), hex(), oct(),
str(), etc .
Example of explicit typecasting

• string = "15"
• number = 7
• string_number = int(string) #throws an error if the
string is not a valid integer
• sum= number + string_number
• print("The Sum of both the numbers is: ", sum)

• ### Output:
```
• The Sum of both the numbers is 22
2-Implicit type casting:

• Data types in Python do not have the same level i.e. ordering of data
types is not the same in Python.
• Some of the data types have higher-order, and some have lower order.
• While performing any operations on variables with different data types
in Python, one of the variable's data types will be changed to the
higher data type.
• According to the level, one data type is converted into other by the
Python interpreter itself (automatically). This is called, implicit
typecasting in python.
Example of implicit type casting:

# Python automatically converts


# a to int
• a = 7
• print(type(a))

# Python automatically converts b to float


• b = 3.0
• print(type(b))
# Python automatically converts c to float as it is a float
addition
• c = a + b
• print(c)
• print(type(c))
``
• ### Ouput:
```
• <class 'int'>
• <class 'float'>
• 10.0
• <class 'float'>

You might also like