What are python data types for beginners
In this article we will be discussing about the data types of python,various operator used in python programming,expressions of python and mutable and immutable types of python and about the math module of python.
Serial no.
|
Topics
|
1
|
Data Types
|
2
|
Mutable and Immutable types
|
3
|
Python object’s key attributes
|
4
|
Operators
|
5
|
Expressions
|
Starting with the data types of python.
Data Types
What are data types ?
Data types are the items that give the variable or objects their original identity. Data types provide the information which operator or commands can be used on the object.
Python supports built-in core datatypes given below :-
Data types are the items that give the variable or objects their original identity. Data types provide the information which operator or commands can be used on the object.
Python supports built-in core datatypes given below :-
- Numbers
- String
- List
- Tuple
- Dictionary
Lets start from the numbers
1] Numbers
Number data types are used to store Numeric values in python.
Python has following number datatypes :-
- Integers
- Integers
- Booleans
- Floating Point Numbers
- Complex Numbers
Integers
Integers are the whole numbers with no fractional parts or no decimal point.
Integers can be positive or negative.
for example :- 25,38,45,-45,-12
Integers are of two types :-
Integers These are the normal integers . Integers in python 3.x can be of any length and the datatype for normal or simple integers is int.
Booleans Booleans represent the two truth values True or False.
In boolean the True represent 1 and False represent 0.
Boolean are one of Keyword which start with a capical letter, otherwise others generally start with small letters.
The int() function can convert a value to integer. It can convert a float value to integer. On converting a string value using the int() function python raises an error.
Floating Point Numbers
Floating Point Numbers are the numbers having a fractional part or having a decimal point.
A integer with a decimal point at the end is an floating point number.
for example :- 12.25 , 10.00 , 2. , 1.44 , 3.414 etc are floating point numbers
Floating point numbers have following advantage over the integers :
- Floating Point Numbers can give values between integers.
- Floating Point Numbers have a much greater range of values
A Floating Point Numbers have a precision of 15 digits in python often called double-precision in python.
In Python, Floating point numbers have also a disadvantage over integers that Floating point operations are generally slow than integer operations.
Complex Numbers
Python also offers you to represent a complex number in numeric type.
A complex number is in the form of a+bj where a is the real value and b is the imaginary value and j (or you can write J) represent the square root of -1 .
Python represent complex numbers as a pair of floating point numbers.
Given Below are some examples of complex numbers in python :-
5+2j
0+3j
2.2+5j
1.1+2.2j etc,.
You can also seprate the two components of complex numbers in python using some attributes given below.
let k=3+2j
k.real gives you the real part of this complex number
k.imag gives you the imaginary part of this complex number as a float.
These two attributes are read-only attributes.
2] Strings
What are strings in python ?A string in python is a sequence of character enclosed in quotes. And each character can be accessed individually using its index.
In strings you can write any charcter like letters,numbers, symbols or all etc
Indexing in string is in two ways
forward indexing starting from 0
backward indexing starting from -1
You can access any character from its index .
for example:-
let a string , channel='CHAMMYCODE'
so for accessing characters we use square backets with the variable name.
channel[0] will give 'C'
channel[7] will give 'O'
channel[3] will give 'M'
channel[-1] will give 'E'
channel[-8] will give 'A'
To check the length of a string you can use the len() function. Sometimes index are also called subscript.
Strings are immutable data type. It means if a string is made then you can not modify,change or alter that string in any way.You can copy that string but you can't change it.
3] lists
What are lists in python ?
A list of Comma separated value of any datatype between square brackets are lists in python.
Indexing in lists is similar to indexing in strings.
Lists are Mutable data types. It means after creating a list you can alter, modify it.
for example :-
python=[3,9,'assd',5,10,-1]
so for indexing
python[2] will give you 'assd'
python[-2] will give you 10
Lists in python can also be nested lists.
What are nested lists in python ?
A list enclosed in a list are called nested lists in python.
For example:-
topic=[1,2,3,['p','y','t','h','o','n'],38,'chammycode' ]
4] Tuple
What are Tuple in python ?
A list of comma separated value of any datatype between parenthesis are tuple in python.
for example :-
q=(1,2,3,4,5,6,7,8,9,10)
Tuple are immutable data type. It means if a string is made then you can not modify,change or alter that Tuple in any way.
5] Dictionary
What are Dictionary in python ?
A dictionary in python is an unordered set of comma-seprated key:value pairs between curly brackets { } .
For example :-
k={1:10,2:20,3:30,4:40}
Here,
1,2,3,4 are the keys
and
10,20,30,40 are the values.
Indexing in dictionary is in some diffrent way.
In places of indexes or numbers for indexing in dictionary the keys are used as indexs.
The keys of dictionary should be unique , no two keys can be same.Keys should be of immutable type.
Dictionary are of mutable data type.
0 Comments