Variables and Datatypes in Python – SparkEasy

 

Variables and Datatypes in Python

 

Topics covered in the article “Variables and Datatypes in Python”:-

1. Comments in Python 2. What is Data? 3. Variables in Python 4. Dynamic Type 5. type() Function in Python 6. Data Types in Python 7. Python Memory Management 8. print()

 

variables-and-datatypes-in-python
© Provided by SparkEasy
#Variables and Datatypes in Python

 

1. Comments in Python

Comments are used in every programming language.

In Python, we first create a .py file (source code). Then there is Python’s compiler which compiles it and converts it into bytecode. It is Python-specific bytecode and its extension is .pyc.

The file in which you are coding is called the source file and in Python, the .py denotes the source file.

 

Now, Who reads the source file?

The source file is either read by the compiler or read by a programmer.

 

Look, the compiler is a program, it has to directly translate the source code into bytecode. And whatever is in the bytecode will be executed by the Just in Time Compiler.

 

But if a programmer is reading that source file, then why would he read it?

He is probably reading the source code because he has to read the program and understand what is the logic of this source file or he has to make some changes to it.

Now, If a person will read this source file, then the person who has created this source file will also want to write some comments while writing the source code so that it is easy to read and understand.

 

So, What are the comments in Python?

  • Comments mean that statement that is not for the compiler to read but for a human being or a programmer to read.
  • Comments are written inside the file so that when a human reads this file, then the purpose of the written code can be easily cleared.
  • Comments are the text that the compiler will ignore.

 

Now, we write comments in the source file in two ways:

i) Single-Line Comment

Whatever text you write by putting the # symbol becomes a single-line comment in Python.

# This is a Single-Line comment.

 

ii) Multi-Line Comment

To write a multi-line comment, we put “”” and then write our text in the middle and after that again use “””. In this way, we can write multiple-line comments in Python.

“””

This is a

Multi-Line

Comment.

“””

 

2. What is Data?

Data is any piece of information that is used by the program to accomplish a task.

Examples of tasks

~ Find the Sum of two Numbers

~ Check whether a given number is even or odd

~ Find LCM of two numbers

All the above tasks can be done only by using some data. It totally depends on the program, about the kind of data you are going to deal with.

Variety of Data

i) Integers
Age of a Person – 24

ATM PIN – 0005

Number of Students – 90

 

ii) Real
Price of a Book – 210.62

Body Weight – 58.5

 

iii) Strings
City Name – “New York”

Person Name – “Alex”

Book Title – “Core Python”

 

There are many types of data, some of them are fundamental and others are of secondary categories. This varies from program to program.

 

3. Variables in Python

Variables are used to hold data during the execution of the program.

In C and C++, you need to declare variables only after declaration you can use them. These are statically-type languages.

int a;

float b;

 

In Python, you don’t declare variables. If there is a need for a variable you think of a name and start using it as a variable.

C/C++ Python
int a;  // Declaration

a=5;

 a=5; # Automatic Declaration

 

 

Variable Name

  • The variable name is any combination of alphabet, digit, and underscore.
age=40         

city2=Surat   

x$=25            X

x.y=5             X

pin_1=3943   

 

  • The variable name cannot start with a digit.
2x=5             X

 

  • Variable names are case-sensitive.

There is a difference between UPPERCASE and lowercase letters when deciding variable names.

A=5 and a=5 are two different variables.

 

  • Keywords cannot be used as variable names.
global=54  X  # global is a keyword in Python.

 

Note: You can delete any variable using the del keyword.

del age      # the age variable will be deleted now.

 

4. Dynamic Type

This means that a lot of things in Python are decided at runtime. Not only the value of a variable may change during program execution but the type as well.

x=5                     # type of x is int

x=5.8                  # type of x is float

x=True                # type of x is bool

x=”SparkEasy”    # type of x is str

Note: Here the value of x changed from int type to str type. So, now the valid value should be x=”SparkEasy”. And the rest of the values like 5, 5.8, and True would be the Garbage Block as no variable is referring to them.

 

C and C++ are statically typed languages. So, the execution time is faster than that of Python.

 

5. type() Function in Python

type() is a predefined function that returns the data type of a specified variable.

a=5

type(a)

 

DataType is always a class in Python.

In the Python shell, you can check the type and id of data by entering type(variableName) and id(variableName):

type(a)

Output: <class ‘int’>

id(a)

Output: 45485454212

Note: Variables of the same type also have a different ID.

 

6. DataTypes in Python

Numeric Types

int

float

complex

5

4.2

2+5j

 

Boolean Type

bool True

False

 

Text Type

str “SparkEasy”

‘SparkEasy’

“””SparkEasy”””

”’Sparkeasy”’

 

Sequence Types

list

tuple

range

[“apple”, “grapes”, “cherry”]

(“apple”, “grapes”, “cherry”)

range(5)

 

Mapping Type

dict {“name” : “Joe”, “age” : 26}

 

Set Types

set

frozenset

{“apple”, “grapes”, “cherry”}

frozenset({“apple”, “grapes”, “cherry”})

 

Binary Types

bytes

bytearray

memoryview

b”SparkEasy”

bytearray(4)

memoryview(bytes(3))

 

None Type

NoneType None

 

Note: double and char data types are not there in Python.

 

 Also Read: 12 Best Features of Python Programming Language

 

7. Python Memory Management

x=5

y=2.6

x=7

Namespace Stack Private Heap Space
Reference Variable

x

y

x

Object

5 → Garbage Block (released by garbage collection)

2.6

7

 

Garbage Collection

It is a program, invoked by Python itself, whenever required, where a job is to release the memory of Garbage blocks (Objects which are not referenced by any name).

What is an Object?

  • An object is a real-world entity. It is an instance of a class.
  • An object is a proper noun but Class is a common noun.

A name in a namespace is a variable that is used to contain the id (reference) of:

i) Instance object

ii) Function object

iii) Class object

In Python, everything is an object.

 

8. print()

Print Simple text

print(“SparkEasy”)

 

Print variable value

print(x)

 

Print Expression

print(x+5*3)

 

Print multiple values

print(x,y)

 

#Variables and Datatypes in Python

1 thought on “Variables and Datatypes in Python – SparkEasy”

Leave a Comment