#2 Python Tutorial for Beginners

 Hello Guys,

     In previous tutorial you learned about python , python feature ,  Installation , python IDE , python basics. In python basic you learned about modules , pip , Comments.

     In this tutorial you will learn below points:

  • Variables
  • Data Types
  • Operators
  • type() function
  • Typecasting
  • input() function

Let's get started.

Variables

A Variables is reserved memory location to store values. In other words,  A variables is the name given to a memory location in a program.

Creating Variables: 

Python has no command for declaring a variable.  A variable is created the moment you first assign a value to it.

Rules for defining variables: 

  1.  A Variable name can only start with an alphabet and underscore.

  2.  A Variable name can contain alphabets , digits and underscore.

  3.  A Variable name can't start with a digit.

  4.  No While Space is allowed to be used inside a variable name.

  5.  Also applies to other identifiers.   


Data Types

Primarily there are following data types in python.
  1. Integer
  2. Float
  3. String
  4. Boolean
  5. None

Python is a superb language that automatically identifies the type of data for us.

Example:
a = "infopsh"   # String
b = 34          # Integer
c = 45.3        # Float 
d = True        # Boolean

# Printing the variables
print(a)
print(b)
print(c)
print(d)

# Printing the type of variables
print(type(a))
print(type(b))
print(type(c))
print(type(d))

Output:
infopsh
34
45.3
True
<class 'str'>
<class 'int'>
<class 'float'>
<class 'bool'>


Operators

Operators are used to perform operations on variables and values.

Python divides the operators in the below groups:
  • Arithmetic Operators
  • Assignment Operators
  • Comparison Operators
  • Logical Operators


Arithmetic Operators :

Example :
# Arithmetic operators 

# --> Addition operator
sum = 23 + 23
print("Addition: " , sum)

# --> Subtraction operator
sub = 25 - 20
print("Subtraction: " , sub)

# --> Multiplication operator
# --> Here * symbol used for multiplication
multi = 5 * 20
print("Multiplication: "multi)
 
# --> Division operator
div = 20 / 5
print("Division: "div)

# --> Modulus operator
mod = 3 / 2
print("Module: "mod)

# --> Exponentiation operator
# --> Here we used double * symbol for performing Exponentiation operator
# --> These operator is used to raise the first operand to power of second.
expo = 2 ** 3
print("Exponentiation: "expo)

# --> Floor division operator
''' --> Here we used double // symbol for performing Floor division operator
        These operator is used to find the floorof the quotient 
        when first operator is divided by the second  
'''
fd = 3 // 2
print("Floor division " , fd)

Output :
Addition:  46
Subtraction:  5
Multiplication:  100
Division:  4.0
Module:  1.5
Exponentiation:  8
Floor division  1

Assignment Operators :

Example :
# Assignment operator

# --> Assign operator
''' --> This operator is used to assign the value of 
        the right side of the expression to 
        the left side operand. 
'''
c = 25 + 25
print("Assign: " ,c)

# --> Add Assign operator
''' --> This operator is used to add the Right side operand with 
        the Left side operand and then assigning 
        the result to the Left operand.
'''        
A = 10
B = 20
A += B
print("Add Assign: " , A)

# --> Subtract Assign operator
''' --> This operator is used to subtract the right operand from 
        the left operand and then assigning 
        the result to the left operand.
'''        
a = 20
b = 10
a -= b
print("Subtract Assign: "a)

# --> Multiply Assign operator
''' --> This operator is used to multiply the right operand with 
        the left operand and then assigning 
        the result to the left operand.
'''        
n1 = 10
n2 = 20
n1 *= n2
print("Multiply Assign: " , n1)

# --> Divide Assign operator
''' --> This operator is used to divide the left operand with 
        the right operand and then assigning 
        the result to the left operand.
'''
no1 = 20
no2 = 10
no1 /= no2
print("Divide Assign: " , no1)

# --> Modulus Assign operator
''' --> This operator is used to take the modulus using the left and
        the right operands and then assigning 
        the result to the left operand.
'''        
a %= b
print("Modulus Assign: " ,a)

# --> Floor Assign operator
''' --> This operator is used to divide the left operand with 
        the right operand and then assigning 
        the result(floor) to the left operand.
'''        
no1 //= no2
print("Floor Assign: " , no1)

# --> Exponent Assign operator
''' --> This operator is used to calculate 
        the exponent(raise power) value using operands and 
        then assigning the result to the left operand.
'''        
A **= B
print("Exponent Assign: " , A)

# --> Bitwise AND Assign operator
''' --> This operator is used to perform Bitwise AND on both operands 
        and then assigning the result to the left operand.
'''        
n1 &= n2
print("Bitwise AND Assign: " , n1)

# --> Bitwise OR Assign
''' --> This operator is used to perform Bitwise OR 
        on the operands and then assigning result to the left operand.
'''        
n1 |= n2
print("Bitwise OR Assign: " , n1)

# --> Bitwise XOR Assign
''' --> This operator is used to perform Bitwise XOR on 
        the operands and then assigning result to the left operand.
'''        
a ^= b
print("Bitwise XOR Assign: " , a)

# --> Bitwise Right Shift Assign
''' --> This operator is used to perform Bitwise right shift on 
        the operands and then assigning result to the left operand.
'''        
A >>= B
print("Bitwise Right Shift Assign: " ,A)

# --> Bitwise Left Shift Assign
''' --> This operator is used to perform Bitwise left shift on 
        the operands and then assigning result to the left operand.
'''        
x = 3
y = 5
x <<= y
print("Bitwise Left Shift Assign: " , x)

Output :
Assign:  50
Add Assign:  30
Subtract Assign:  10
Multiply Assign:  200
Divide Assign:  2.0
Modulus Assign:  0
Floor Assign:  0.0
Exponent Assign:  348678440100000000000000000000
Bitwise AND Assign:  0
Bitwise OR Assign:  20
Bitwise XOR Assign:  10
Bitwise Right Shift Assign:  332525673007965087890625
Bitwise Left Shift Assign:  96

Comparison Operators :

Example :
# Comparison Operator
# --> Comparison Operator also known as Relational Operator

# --> Greater than
''' --> This operator returns True if the left operand 
        is greater than the right operand.
'''
print("Greater than: "5 > 2)

# --> Less than
''' --> This operator returns True if the left operand 
        is less than the right operand.
'''
print("Less than: "5 < 2)

# --> Equal to
''' --> This operator returns True if both the operands 
        are equal i.e. if both the left and the right operand 
        are equal to each other.
'''        
print("Equal to: " , 5 == 5)

# --> Not Equal to
# --> This operator returns True if both the operands are not equal.
print("Not Equal to: " , 5 != 3)

# --> Greater than or equal to
''' --> This operator returns True if 
        the left operand is greater than or equal to the right operand.
'''        
print("Greater than or equal to: "5 >= 5)

# --> Less than or equal to
''' --> This operator returns True if the left 
        operand is less than or equal to the right operand.
'''        
print("Less than or equal to: " , 9 <= 5)

Output :
Greater than:  True
Less than:  False
Equal to:  True
Not Equal to:  True
Greater than or equal to:  True
Less than or equal to:  False

Logical Operators :

Example :
# Logical Operators

# --> and operator
# --> Returns True if both statements are true
x = 5
print(x > 3 and x < 10)

# --> or operator
# --> Returns True if one of the statements is true
x = 5
print(x > 3 or x < 4)

# --> not operator
# --> Reverse the result, returns False if the result is true
x = 5
print(not(x > 3 and x < 10))

Output :
True
True
False


type() function :

type() function is used to find the data type of a given variables in python.

Example :
a = 12    # Integer data Type
b = True  # Boolean Data Type 
c = "PSH" # String Data Type 
d = 123.3 # Floating Data Type

# Here type() function is used to return data types of specific values

print(type(a))       
print(type(b))
print(type(c))
print(type(d))

Output :
<class 'int'>
<class 'bool'>
<class 'str'>
<class 'float'>


Typecasting :

Typecasting means one data type converted into  another data type. Here are many functions to convert one data type to another data type.

Example :
a = "35"
a = int(a)
print(type(a))

Output :
<class 'int'>


input() function :

  • This function allows the user to take input from the user as string.
  • This function return output of input is always a String.
Example :
# input() function program
# Take Value from user

num1 = input("Enter number1: ")
num2 = input("Enter number2: ")

n1 = int(num1)
n2 = int(num2)

print("Sum: "n1 + n2)

Output :
Enter number110
Enter number220
Sum:  30


Note : Here In this tutorial above code written in VS Code Editor.



If you liked this post do comment ,share and promote the post 🙏 . Please stay with us and support.  🙏

   

Post a Comment

0 Comments