Jan 12, 2020

Data Types In Python

Data Types In Python

this post is all about the data types in python explained with examples and images for a better understanding of this concept.

When we are working on any project, It's very important to understand what type of data you are working with so that you can process that data in an appropriate format. If you don't process data in an inappropriate format you might face errors and bugs.

What are the different Data Types in Python

DIFFERENT DATA TYPES IN PYTHON
DATA TYPES IN PYTHON
The above diagram shows the different data types in python but each data type have different types and some integral data types lets see one by one below this.

NONE Data type in python

When you have a variable and that variable is not assigned with any value that is called NONE. in different languages we use NULL but in Python we use NONE. this data type defines that the variable doesn't hold any value as of now.but later you can change the value in None Variable.
  • None is not 0
  • It's not an empty string at all
  • None doesn't represent False 

SYNTAX: NONE


Examples


>>> a =None

>>> print("",a);
 None
>>> print("The value of a is",a);
The value of a is None

Numeric Data type in Python

As its name suggests,This is the data type which stored numbers and we know there are different types of numbers like natural, complex, decimal, etc.

Similarly, Python have different data types in the Numeric Data type Section.
Numeric Data types in Python
Numeric Data type

int Data type in Python

an int data type is deal with the whole numbers(0,1,2,3,4, etc), these numbers can be positive or negative, also numbers don't have any decimal points.

In C language size of int is 4 bytes but in python3 the size of int is (24 bytes) unlimited and in python2 we can use long with int to increase the size of the data type.

>>> num =13
>>> num2=14
>>> num3=16
>>> num+num2+num3
43




float Data type in Python

Float values are number with decimal points, like C and C++ we don't have to write the data type in python we can directly write the number without mentioning its data type.


>>> a=14
>>> b=16
>>> c=1.412
>>> d=19.65
>>> a+b+c+d
51.062

As you can see from above the answer, it has decimal point and we did not mention float data type anywhere in the above code and still get the answer as a float value


Complex Data type in Python

First, we need to know, What is a complex number?
Complex number is written in the form a+ib where "a" is called the real part which is (1,2,3,4,5, etc) and "ib" is called the imaginary number (i=under root -1).
But, In python, we use "j" instead of "i".
complex data type typically used in mathematical functions.

>>>14+9j
>>>19+4j


Boolean Data type in Python

Boolean data type is have two options either it will be true or false, in python we define boolean with True or False keywords. 
"Remember that the intial letter in True and False in booloean Data type must be upper Case or the compiler will give error"
>>> a=4
>>> b=9
>>> b>a
True
>>> a>b
False
>>> bool =a>b  Here we have assigned the Boolean to the bool and we give one condition to that variable and when we press enter the value in bool will be either True or False
>>> bool
False  As you can see here the value in bool is false as per our condition.
>>> a+8>b
True
>>> a+3<b
True
>>> a+9<b
False

"The common boolean operators are given below"

  • and
  • not 
  • or
  • ==(Equal to)
  • !=(Not equal to)

>>> X=True
>>> Y=False
>>> X or Y
True
>>> Y or X
True
>>> Y and X
False
>>> X==Y
False
>>> X!=Y

True
>>> not X
False
>>> not Y
True


X
Y
X==Y
X or Y
X and Y
True
False
False
True
False
False
True
False
True
False
True
True
True
True
True
False
False
True
False
False

True posses value 1
False posses value 0



List in Python

List is same as array(the array is the collection of data of same data type) but list not necessarily of homogeneous data type that why this quality makes it one of the most powerful tool in python language.


Some facts about list

  • we can use integer, float, string, etc data type in one list.
  • List are mutable in nature ,Hence we can change values as per requirement.
  • List is Ordered
  • List have Definite count
  • List uses concept of Index number(https://www.digitalzak.com/2020/01/pythonlistappend.html) To know what is index number you can visit this link.
  • Duplication of the element in the list is allowed because index numbers are fixed for each element.
Indexing values
indexing in python



Example

>>> list=[12,13,14,15,18,12,12,16]
>>> print(list[3])
15
>>> list
[12, 13, 14, 15, 18, 12, 12, 16]
>>> digitalzak=["Zeeshan","MBA","Data","Python"]
>>> print(digitalzak[2])
Data
>>> print(digitalzak[0])
Zeeshan

We Can add values and remove values from the list by using certain functions and to know more about it you can visit the link.





Tuple in Python


Tuple in Python Link this is link

Visit this link for Tuple with example and overview.

Range in Python 

The range function is used to show the range of starting to the end of the series, for example, range from 1-6
then it will show 1,2,3,4,5,6 this numbers.

By using LIST function we can print the entire range.


"Thanks for reading for more post on python you can follow my blog so that you will notify about new post"

Jan 9, 2020

TUPLE IN PYTHON

TUPLE IN PYTHON

THIS POST CONSIST OF TUPLE IN PYTHON AND HOW TO USE TUPLE IN PYTHON LANGUAGE WITH EXAMPLES AND WRITTEN CODES IN PYTHON LANGUAGE FOR BETTER UNDERSTANDING OF PYTHON TUPLE.

Tuple is the same as the list but here we cannot change the values as the values in the tuple are fixed and we cannot performs a function like extend, append, insert, etc in this list.

Why it is important to use a tuple.this is the most obvious question coming into your mind as you know there are certain projects or tasks we perform in our life when we want the values that we have assign should not be manipulated by anyone that time we can use Tuple as this function didn't allows users to change the values from the list.

Syntax of Tuple in python

"In the list, we used square brackets [] but in the tuple, we are using the circular brackets ()."
>>> tup=(12,131,45,46,15,54,35,11) tuple list
>>> tup[3] checking the list by entering index number
46               At index number 3 we have 46
>>> tup[6]
35
>>> tup[0]  At index number 0 we have number 12
12 


let's see one more example and this time we are going to use the string instead of numbers so that you can understand better.


>>> planet=("earth","jupiter","mars","mercury")

>>> planet[2]

'mars'
>>> print(planet)  by using the print function we are printing the all the values present in the tuple list.

('earth', 'jupiter', 'mars', 'mercury')
As you can see from above that the output is showing all the values in the tuple list.

Using different functions in Tuple

we can perform certain arithmetic operations on the tuple to show the desired element from the tuple list or performing some functions like printing the desired numbers of results like first 5 elements or the last 3 elements like that.


If you don't know how to perform these arithmetic operations, What is the syntax for this, then you can refer to my post in which I have explained all the points related to arithmetic operations in python.


>>> planet
('earth', 'jupiter', 'mars', 'mercury')

>>> print(planet[1:3])
('jupiter', 'mars')

Assigning the range to print.

How to find length of the tuple in python

to find the length of the tuple means finding the number of elements in the list of tuple we are using the function called "Length" and how to use that function is written below,

>>> planet

('earth', 'jupiter', 'mars', 'mercury')

>>> print(len(planet))
4

As you can see from the above example we are using the function Length and to use that function we have to provide the parameter to function called len by using this function we can find the length of the tuple or any other list in terms of an integer that is a number of elements in the list.

tuple have various applications as it is used to define the values which are not going to change throughout the process so that any operation cannot change the values present in this tuple list.

thanks for reading for complete tutorials for python you can visit my page "python tutorials" at the top of this blog thanks for reading.






Jan 6, 2020

[2020 UPDATED] PYTHON LIST APPEND || PYTHON LIST APPEND LIST

Python List Append, Extend, Insert, Remove


This post is all about the String and python list append, extend, clear, remove, insert, pop, reverse, count and copy But we focused here on PYTHON LIST APPEND this is our topic.


In python, we can use a different kind of string in a single command, not like C and C++ languages where we have to use an array and we have to mention the data type of the array, here we have to just give the name to the array and we can use different type of data type in the single list like in single list name[] I can use the data types likes float, int, char, etc.

The syntax for assigning the values 

num = [11,12,34,15,54,23,45,22,63,47,13]
here we have used the variable name ''num''' and by using equal to sign we are assigning the values to the variable "num" but you need to remember that for assigning multiple values to num you have to use Square bracket and by using ","(comma) we can assign multiple values in python. this is the syntax which we should remember if we want to use python list append, extend, insert, remove in programing.

If you look at the above example where we use num if we have to call the specific number from that list or if we have to exclude some number from that list then we can use the one simple syntax.

How to call the values from the list

name_of_variable[index_number]
let's take same example
num = [11,12,34,15,54,23,45,22,63,47,13]

Indexing in the python language is start with "0"
so from the above list

11-index number -0
12-index number -1
34-index number -2
54-index number -3
23-index number -4
45-index number -5
22-index number -6
63-index number -7
47-index number -8
13-index number -9

let's have some codes from IDLE python 3


>>> num = [11,2,35,14,54,36,48,12,36]

>>> num[0]               here we called the value by index number
11                               we got the 11 because 11 is on index no. 0
>>> num[1]
2
>>> num[6]
48

>>> num[9]
Traceback (most recent call last):
  File "<pyshell#4>", line 1, in <module>
    num[9]
IndexError: list index out of range

   here we get en error because our range is from 0-8 and we are asking the value which is not present in our list so that it gives error so remember that when you are calling any value it should be in the range of given list.

If You Want To Print in Between values from List

If you have the list num = [11,12,34,15,54,23,45,22,63,47,13] and you want to print the number from 34-63 what should you do or if you want to print the number from 15 till the end then you have use the syntax which is given below,
  • I want to print the numbers from 34-63
  • 34 index number is 2
  • 63 index number is 8
  • Syntax :  num[2:9]
  • Output: [34,15,54,23,45,22,63]
  • why we give index number 9 as we want the numbers till 8
  • that means it excludes the value at 9 and print the value just before it.
Let's perform some code then you can understand that Blue Colour output and Black Colour is input in the below program.


>>> number= [11,36,14,86,45,12,365,44,56,87]

>>> number[3:9]
[86, 45, 12, 365, 44, 56] number shows from 3-8

>>> number[2:6]           
[14, 86, 45, 12]    Numbers shows from 2-5

>>> number[7:8] Number shows from 7-7 that means one number
[44]

>>> number[5:]
[12, 365, 44, 56, 87] what is different here we did not assign any value after ':' that means it shows all the values after 5 as we did not mention the limit here.

>>> number[:3]
[11, 36, 14]
Here we didn't mention the index number so that it shows the number from starting to the later limit which is 3.

>>> number[2:]
[14, 86, 45, 12, 365, 44, 56, 87] after 2 we are getting all results.

Using String in List

As I mentioned earlier you can use Strings in the python list lets directly see the syntax for that

Syntax: Variable_name = [list values]

example:- 
>>>universe =['moon','star','sun','mercury','digitalzak']
>>> universe[3]
'mercury'
>>> universe[1]
'star'

OPERATIONS USING LIST

  • append
  • insert
  • clear
  • remove
  • pop
  • reverse
  • count
  • copy
Syntax

let's make the one list called "num1"

num1 = [1,3,5,7,9,6,3,4,5]
"We are going to use all of the above operations on list "num1" for better understanding"
"For using this functions we have to use the list_name.operation_name(parameters)" 

Python list APPEND

This function is used to add the new value in the given list at the end of the list


>>> num1 = [1,3,5,7,9,6,3,4,5]
>>> num1.append(22)
>>> num1
[1, 3, 5, 7, 9, 6, 3, 4, 5, 22]  22 is added at the last of the list

Python list INSERT

By using this function you can add any value at a specific index number the syntax is num1.insert(index_no , Value)

>>> num1
[1, 3, 5, 7, 9, 6, 3, 4, 5, 22]  this is  over given list

>>> num1.insert(3,71)  By using the .insert fucntion we have inserted the 71 at the index number 3.

>>> num1
[1, 3, 5, 71, 7, 9, 6, 3, 4, 5, 22] when we press enter we get the result as you can see from the above output that the 71 is present at the 3rd index number.

Python list Remove

this function is used to delete any number or string from the list in python it is very important for using the list in python because sometimes we have to make a program in which some entities have to left the list then we can use this function for that.

We can do that by giving that entity directly or by giving the index number for that entity.


>>> num1

[1, 3, 5, 71, 7, 9, 6, 3, 4, 5, 22]  This is our updated list where we added the number 71 now I want to remove that number we can use the remove function here.

>>> num1.remove(71) Here I am directly giving the name or value of the entity.

>>> num1
[1, 3, 5, 7, 9, 6, 3, 4, 5, 22]  Number 71 is removed form the list

"you cannot remove value by index number in remove function for that we need .pop function"

Now let's try to remove 9 by its index number because you can see 3 comes two times in list so that it's important to give the index number to show the compiler .the number you are interested to remove.


Python list pop

this function is used to remove the value from the list by its index number lets see it by the example.


>>> num1

[1, 3, 5, 7, 9, 6, 3, 5, 22]
>>> num1.pop(4)
9   Here it shows which number is excluded from the list
>>> num1
[1, 3, 5, 7, 6, 3, 5, 22]

What will happen if we use .pop without index value

if we use .pop function without index value it automatically removed the last element from the list.
>>> num1
[1, 3, 5, 7, 6, 3, 5, 22]
>>> num1.pop() here I didn't give any index number so it remove last element from the list which is 22 as you can see in the output it shows below.
22
>>> num1
[1, 3, 5, 7, 6, 3, 5]  the last element is removed from the list.


As you can see that the number 9 is removed by .pop function by using the index number.

This is all for the today's post hope you enjoy learning the python list append, insert, remove and all python is one of the most trending languages nowadays and it is also the one of the easiest languages which can be learned by anyone if you want to learn more about the python then you can follow my blog and also you can give comment below if you have any difficulty regarding python I will try to give my 100 percent to answer your question thank you soo much.






Jan 5, 2020

python arithmetic operators

This article all about the arithmetic operators and their operation in python the following tables and coding shows some examples of how to use the arithmetic operators in python.

PYTHON ARITHMETIC OPERATORS

Arithmetic operations are the mathematical function which is used to perform functions like Addition, Subtraction, Multiplication, and Division.

Table to show operations and operators


Operation
operation
Syntax for operation
+
Addition of two operands
a+b  , 8+9  , 8+2
-
Subtracts two operands
a-b , 8-9 ,8-2
*
Multiplication of two operands
a*b, 32*5
/
Division of two operands /Float(ans)
a/b  ,32/4
//
Davison of two operands/int(ans)
x//y , 33/4 , 10/3
%
Modulus or shows the remainder
10%3
**
For giving the power to the number
2**3 means 23

Addition operator examples in python(+)

Remember that >>> this is the inputs given by the user and blank space below this is the computer-generated answer or output

>>> 2+3 here we just added two number then press enter
5              then we get the output
                                       
>>> a=5 we assign variable "a" as 5
>>> b=6 variable "b" as 6
>>> a+b= and here we added this variables
11  wow we get the asnwer as integer after pressing enter.

>>> a+b+11 here we already assign the value of "a,b" and added an integer number to it that is 11.
22 we get the result as 22 because "a=5, b=6" we already define this variable

>>> 132+145+a+b here we have added two big numbers with our already assign values number then we get the desired result.
288
As you can see the python has very user-friendly coding system which can be used by anyone quite easily this codes come from IDLE of python3 so hope you will enjoy the later part of this blog where all the operations are used in quite complex equation which can help you to understand the all python arithmetic operators.


Subtract Operators in python(-)

>>> 354-45
309

>>> 264-245
19

>>> x=6
>>> y=9
>>> x+y
15

>>> x+y-10
5

Multiplication Operators in python(-)

Multiplication operator is also very easy as you just have to put * for multiplication of two numbers.

>>> 14*2
28
>>> 14*36
504
>>> x*y
54

Division Operators in python(/)

This is a very important operator in python you need to understand the difference between integer number and float number.

Integer Numbers : 1  , 2 , 3,4,5,6,7,8,9,10..
Float Number: 12.00, 11.36 , 14.29 , 11.78 ,19.37..

>>> 14/2
7.0
>>> 1123/135       When we use the / operator we are dividing the number and answer is coming with the decimal point
8.318518518518518
>>> 112/4
28.0
>>> 126/48
2.625  this answer can be written as 2 + 0.625 that 0.625 is the decimal value and by using / we are getting the decimal answer sometimes it may be whole number 7.0 but it still shows .0 that means it is showing the integral value.

Division  Operators in python for INTEGER VALUE(//)

In the above example, you have seen the result is coming in decimal form like if we divide 10 with 3  we will get the answer as 3.33 and but what if someone wants to divide the number but didn't want the answer in a decimal form then he can use the // operator  lets see the example

>>> 10/3        By using single divisional operator
3.3333333333333335  float

>>> 10//3    By using double divisional operator
3         integer


Modulus  Operators in python (%)

Modulus operator is used to determine the remainder for example if you divide 10 by 3 then you will get the remainder 1 as 10 cannot be divided by 3 with zero remainder so if we have to find the remainder we will use the Modulus operator.let's see the programme and understand that.
 
>>> 10%3
1
>>> 1235%23
16
>>> 4523%13
12 

Exponential or power  Operators in python (**)

Suppose I have to write the programme where I have to write the square , cube or 10th to the power n of any number then we can use this operator lets see this in example

>>> 2*2*2*2  Here you can see the 2 is multiplied by 4 times if I want to do that for 100 the time it will be tough to write the multiplication to reduce that effort we are using ** 
16    



>>> 2**4   This Statement means 2 to the power 4  as you can see both results are same
16


thank you for reading if you like my blog please do mention that in the comment section below and do share with your friends and enjoy learning

this is all about the python arithmetic operators.


Popular Blog Posts