Python. Literals. Creation (generation) of an object of some type. Basic types of objects

Literals. Creation (generation) of an object of some type. Basic types of objects


Search other websites:

1. The concept of a literal in Python. Features of the use of literals. Examples of literals

In the Python programming language, a literal is an expression (or constant) that creates (generates) an object. If a literal is found in the program text, then a separate object of some type is created for this literal. That is, the system generates the corresponding code, which creates an object containing the value of this literal. The created object has a certain lifetime.

Unlike other programming languages (C++, C#, and others) in Python, the term “constant” means that literals are not objects that cannot be changed. The so-called constant objects in Python are subject to change.

Examples of literals:

'Hello world!'
[1, 3, [2, 4, 6]]
{ 'Sun', 'Mon', 'Tue', 'Wed' }


2. Object generation. The concept of built-in type of object

In Python, everything that is used in a program is an object. That is, for each line of program code, the system generates a corresponding object. For example, objects are generated for:

  • an integer;
  • character string;
  • a list;
  • a file;
  • a function;
  • a module;
  • a class;
  • other elements of the language.

Each object has its own behavior. Objects are created using instructions and expressions.

The Python language contains a set of native built-in object types. When using this type, an object is generated. For example, when using a string literal in a program

'Hello world!'

an object of string type with the value ‘Hello world!’ is created (generated).


3. Basic (built-in) object types in Python

The following is a list of the most used basic types of objects.

Object type Literal
12, 2.855, 1+2j
'Sunday', "Monday"
[1,2, [3, 4 [5]]], [13, 'Text']
{ 1:"One", 2:"Two", 3:"Three" }
(8, 'bestprog')
filename=open('myfile.txt', 'r')
set('jklmn'), { 'j', 'k', 'l', 'm', 'n' }
Other types
None, True, object templates, etc.

Each created object is associated with its own set of operations. For example, for strings, you can only perform operations on strings; for numeric objects, you can perform valid operations on numbers.


4. Does Python have a type declaration construct for an object?

No, it doesn’t. The type of object to be created is determined by the syntax of the expression being executed. In other words, the syntax of the expression defines the type of object being created and used.


5. What does the term “dynamic typing of an object” mean?

In Python, the term “dynamic typing of an object” means that the data type of the object is determined automatically and that this type does not need to be declared in program code.


6. What does the term “strong typing of an object” mean in Python?

The term “strict typing of an object” means that after creating an object of some type, only those strictly defined operations that are applicable to its type can be performed on this object.


7. Video tutorial


Related topics