Posts

Conditions in Python

                           If....else... Decision making is important part of programming. We come across many situations where we have to choose something during coding like whether it will print success or failure, something like that. Like other programming language, in Python also we have If...else conditions. If true then this else that.  By using If..else we only select the block of code we want to execute. We can also use nested if...else . In nesed if...else we have multiple if..else conditions inside the former conditions which we will see in example. Syntax : if condition:                statements         else:             statements E.g.   x = 10      if x <= 10:           print("x is low.")      else:  ...

Python Dictionary

                            Dictionary A dicitonary is a key-value pair collection which is unordered and can be changed or altered. We can not perform indexing on dictionary like lists but dictionary is still indexed. How? Well, we can use key inplace of index to get the value. We use curly braces { } to represent the dictionary.  E.g. dict1 = {'a':1, 'b':2, 'c':3, 'd':4} The value on left side of colon is called key, and value on right side of colon is called value. Hence key-value pair. We can use any datatype for keys and values. We can also use lists, sets, dictionary,etc. To access the value, we use keys. We can also use get() function for access the value. Just put the key in get() and value is accessed. E.g.   dict1['a']          # output : 1      dict1['c']          # output : 3     dict1.get('b')    ...

Python Sets

                                 Sets A set is a collection of data which is unordered and unindexed. We represents sets with curly braces { }. In sets duplicate items are not allowed.  E.g. set1 = {'apple', 'mango', 'orange' } Items in set are unindexed which means you can not access the item individually from the set using its index. But we can loop through the set. E.g. for i in set1:           print(i)        #output : apple                       mango                  orange set() method is used to define the set. E.g. set2 = set(['one', 'two', 'three'])     print(type(set2))      # output : set In set we can have any type of data but it should be immutable means the data which does not cha...

Python Tuples

                                 Tuples Just like list in Python, Tuple is also used to store the data but the difference is that tuples are unchangeable. Tuples are also ordered. We use round brackets or paranthesis to represent tuple.  E.g. tup1 = ('one', 'two', 'three') Tuple is indexed same as lists. We can access the elements of tuple by using index strtaing from 0. E.g. print(tup1[1])          # output : two Negative indexing means we start from the end of tuple just like list. -1 means last element, -2 means secod last and so on. E.g.    tup2 = (10, 25, 31, 36, 67)     print(tup2[-1])        #output : 67     print(tup2[-3])        #output : 31 To check the length of tuple we can use len() method. E.g. len(tup2)          # output : 5 As we already discussed that tuplea are...

Python Lists

                         Python Lists List is a ordered, indexed and changeable collections of data. List can have heterogenous data type. In python we represent list with square brackets [ ].  Example of list : fruits = ['apple', 'banana', 'mango']              items = ['table', 12, 3.13, 'tree'] Index of list starts from 0. We can check the length if list using len( ) function.  E.g.  numbers = [45, 52, 23, 87]     len(numbers)     # output : 4       numbers[0]        # output : 45      numbers[2]        # output : 23 If we want to access the list from last element and do not know the length then we can use negative indexing. -1 index is of last element, -2 index is of second last and so on.  E.g. numbers[-1]      #output : 87    numbers[-3]...

Data Types

                            Data Types As the name suggests, data type means the type of data you are using like integer, string, float, array etc. Variable is used to hold any type of data. In Python, you do not need to define the type of variable, just assign the value to it and it will automatically be declared as that type of data. In python to check the data type of variable, type() function is used. For e.g. :                    x = 10                type(x)           # int                y = "Hello"                type(y)           # str In python there are many data types like any other programming language. But there are 5 standard data type: Numbers : Number stores numeric values. ...

Python Variables

                     Python Variables Variable is used to store the values. Basically we can think of variable as a container which contains the value like number, string, boolean, etc. Variables are also called as identifier because they are used to identify a particular value. In other programming languages, before assigning a value to variable you have to declare the varibable with its data type like whether it is integer, string, float or any other. But in Python you don't have to do this. Just pick the name of variable and assign the value. The type of variable is automatically detected in Python. Python is smart right. We can even change the data type of variable after they have been set. Suppose you make a varable x and assign the integer value to it, but after some time you are giving string value to the same variable then the type of variable will be change from int to string. I told you Python is smart. For example : ...