Home / Python Basics / Python Programming Variables,Data Types and Type Conversion

Python Programming Variables,Data Types and Type Conversion

In this lesson of Python Programming Language, you will learn what is Python Variables,Data Types and Type Conversion and how to use them properly.

 

 

Python Variables

Almost in every major programming language variables are declared to store values in reserved memory locations. It means whenever a variable you can hold some amount of space in the memory. The interpreter decides what amount of space should allocate in reserved memory based on the data type we have given to the variable. To call any other thing or person we have to give it a name similarly we assign a unique name to every variable to call it. The name we give to a variable is called an Identifier.

Remember: Python does not actually assign the value to the variable but just give the reference of the memory location where that value is stored.

Assigning Values to Variables in Python Programming

By assigning means to give some value to the variable we declared that can be used for some operation. In other languages, you must give a data type to a variable to hold the required amount of space in memory but in Python Programming Language does it implicitly according to the values you assign to a variable.

To assign value the equal sign ( = ) is used. Now on the right side of the ( = ) operator name of variable sets and on the other side is the value of the variable.

We have assigns the values 10, Python, 3.7.2 and WalkintoPC to variable a, Language_name, version, and Website respectively. The output of the above code will be as shown below:

you can replace any value in any variable. see the example below.
For the purpose of proof when we run the code we will get the replaced value like this.
Python allows programmers to assign a single value to multiple variable if they want to do.
The result of the above code will be like this.

Python Data Types

A programme consists of different kinds of a data type to perform some specific task. If we take an example of school management software a student fee is stored as a numeric value or his home address is stored as alphanumeric value etc. Python Programming language supports different data types to work with.

Python Numbers Data Type

Numeric values stores in Python numbers data type.

Python programming not only supports simple numeric value data but also supports following other numeric data types as

  • Python int (signed integers)
  • Python long (long integers, octal, and hexadecimal)
  • Python float (floating point real values)
  • Python complex (complex numbers)

Python Signed Integers

By Python signed integers we mean both positive and negative numbers.

Python Long

By long means, it can store up to the double value of data than the signed integer data type. A long data type can be represented with Octal and Hexa-decimal. By octal the number with base 8 and Hexa-decimal the number with base 16.

Recommended:  First Program In Python Programming Language

Note: We can use as Python Languages allows us to write small alphabet ( l ) for long but for the sake of not having confusion we use capital alphabet ( L ).

Python Float

Python supports floating point number. A floating number means real numbers with decimal values.

Python Complex

To create a complex literal in python we need to create a real and imaginary part both represented by a floating point number.

The output will be as

In above the 7.12j is the total value of variable 7.12 is the real number and 0.0 is the imaginary part.

Python String Data Type

In simple words, a string can be defined as a series of characters enclosed in quotation marks. To enclosed string there can be three types of quotation marks as single quotation ( ‘ ‘ ) , double quotation (” “) and triple quotation ( “””  “””).

In the above illustration, both single quote and double quote use to show string on the same line but in case your string goes to the next line then you need to use triple quote ( “””  “””) to show the string to the output window.

Python Single Character Literal

You can also show a single character or one character from the string by using a single ( ‘ ‘ ) or double (” “) quotation.

Now if you want to access a single character from a complete string you can use slicing operators  ([ ] and [:] )  to access that particular character by accessing their index number. Remember the index number always starts from Zero ( 0 ). In case you want to concatenate or joins two string with one another you can use the ( + ) operator to do so. Moreover, if you want to print the same string more than one time you can type asterisk sign ( * ) with the string as shown below to print it as many times as you want.

The Output will be as:

Python List Data Type

Python List is one of the frequent use data type because of it’s flexibility and ability to change values dynamically. It is not mandatory to have all the elements in the list of the same data type. List enclosed in brackets ( [] ) and each element is separated by a comma (,). You can use slicing operators  ([ ] and [ : ] )  to access any particular element by accessing their index number.

Recommended:  Comments, Indentation and Statement in Python Programming Language

Remember: The index number always starts from Zero ( 0 ). To concatenate or joins two elements you can use the  (+) operator to do so. Moreover, if you want to print the same element more than one time you can use asterisk sign ( * ) with the element as shown below to print it as many times as you want.

Python Tuples Data Type

The tuple is another data type supported by Python programming language nearly similar to the list data type but the major and prominent different is the tuple data type is non-modifiable once it is created it cannot be changed. A tuple is much faster than list but can’t be changed dynamically. It is enclosed in parentheses ( () ) and each element is separated by a comma.  You can use slicing operators  ([  ] and [ : ] )  to access any particular element by accessing their index number.

Remember the index number always starts from Zero ( 0 ). To concatenate or joins two elements you can use the  (+) operator to do so. Moreover, if you want to print the same element more than one time you can use asterisk sign ( * ) with the element as shown below to print it as many times as you want.

The output will be as
As said earlier you cannot change the value of tuple once it is created for a demo see the illustration below.

Python Dictionary Data Types

When we need to work on a project where a huge amount of data going to involved in this case Python comes with a very helpful features name as Python Dictionary. It is defined as a hash table type unordered pairs of key-values. It is very flexible and one can change or adjust it later according to his need.

Dictionaries work in the fashion of pair with key:value. Key can be of any supported data type and the value can be anything but generally, it consists of numbers and strings. Python dictionaries enclosed with braces ( {} ) and a pair of a square bracket ( [] ) is used to get and assigned a value.

Python Data Type Conversion

Python Programming Language supports different data type conversions. There are different built-in functions available in Python for conversion from one data type to another.

Python Int to String Conversion

In below illustration, you will see an example of int to string conversion

Python Float to Int Conversion

In the below illustration, an example is presented to show you how to float to integer conversion is done in Python Programming Language

Other than this there are also different built-in function for conversion which is as follows:

FunctionDescription
int(x [,base])This built-in function of Python use to Converts x to an integer. base specifies the base if x is a string.
long(x [,base] )This built-in function of Python use to Converts x to a long integer. base specifies the base if x is a string.
float(x)This built-in function of Python use to Converts x to a floating-point number.
complex(real [,imag])This built-in function of Python use Creates a complex number.
repr(x)This built-in function of Python use Converts object x to an expression string.

About Jack Sparrow

Avatar
Hay I am Admin Of This Website. I am very passionate about Computers and technology. This website is the result of my passion. I love to eliminate the barriers comes in the for seeking education. This website is the free source of learning about new technology and up to date knowledge.

Check Also

Introduction-Of-Python

Introduction To Python Programming Language

In this Introduction to Python Programming Language you will learn about the world’s dynamic and easy …

Leave a Reply

Your email address will not be published. Required fields are marked *