Python Installation & Data Types

Python Installation & Data Types

#TrainWithShubam #Day13 Task #90 Days of DevOps Challenge

What is Python

Python is Open source, general purpose, high-level, and object-oriented programming language.

Python consists of vast libraries and various frameworks like Django, Tensorflow, Flask, Pandas, Keras etc.

Python is used in developing Web Applications, Machine Learning, Artificial Intelligence etc.

Installing Python

Go to the Python website https://www.python.org/downloads/ choose your operating system download and install.

First Python Code

Hello, World! Program using python.

print("Hello World!")

Data Types

1. Numeric Data Types

  • int - holds signed integers of non-limited length.

  • long- holds long integers(exists in Python 2.x, deprecated in Python 3.x).

  • float- holds floating precision numbers and it’s accurate up to 15 decimal places.

  • complex- holds complex numbers.

      # Creating a variable with integer value
      a = 100
      # Checking Datatype using print
      print("The type of variable having value" ,a, "is" ,type(a))
    
      # Creating a variable with float value
      b = 10.76
      # Checking Datatype using print
      print("The type of variable having value" ,b, "is" ,type(b))
    
      # Creating a variable with integer value
      c = 10+2j
      # Checking Datatype using print
      print("The type of variable having value" ,a, "is" ,type(c))
    

2. String Data Type

  • A combination of Characters or Sequences of Characters combined is known as a string.

  • Generally, Strings are enclosed in either a single quote or a double quote.

      a = 'Hello'
      b = "Welcome to python"
    
      # Concatinating two strings it can be done in two ways 
      print(a,b)
    
      print(a + " " +b)
    

3. List Data Type

  • The list is a versatile data type exclusive to Python.

  • In a sense, it is the same as the array in C/C++.

  • Python is it can simultaneously hold different types of data.

  • The list is an ordered sequence of some data written using square brackets [] and commas(,).

      #list of having only integers
      a= [1,2,3,4,5,6]
      print(a)
    
      #list of having only strings
      b=["hello","john","reese"]
      print(b)
    
      #list of having both integers and strings
      c= ["hey","you",1,2,3,"go"]
      print(c)
    
      #index are 0 based. this will print a single character
      print(c[1]) #this will print "you" in list c
    

4. Tuple Data Type

  • The tuple is another data type which is a sequence of data similar to a list.

  • data in a tuple is write-protected and immutable.

  • Data in a tuple is written using parenthesis and commas.

      #tuple having only integer type of data.
      a=(1,2,3,4)
      print(a) #prints the whole tuple
    
      #tuple having multiple type of data.
      b=("hello", 1,2,3,"go")
      print(b) #prints the whole tuple
    
      #index of tuples are also 0 based.
    
      print(b[4]) #this prints a single element in a tuple, in this case "go"
    

5. Python Dictionary Data Type

  • Python Dictionary is an unordered sequence of data of key-value pair form.

  • It is similar to the hash table type.

  • Dictionaries are written within curly braces in the form key: value.

  • It is very useful to retrieve data in an optimized way among a large amount of data.

      #a sample dictionary variable
    
      a = {1:"first name",2:"last name", "age":33}
    
      #print value having key=1
      print(a[1])
      #print value having key=2
      print(a[2])
      #print value having key="age"
      print(a["age"])
    

6. Python Boolean Data Type

  • Boolean is allowed to evaluate any value.

  • Boolean will return true or false of any expression which is mostly used in test cases.

  • Anything other than null and zero will return true

      # Checking boolean of a string and integer
      bool("Hello")
      bool("10")
      # Above both lines will return True
      bool(False)
      bool(" ")
      # Above both lines will return false