# import numpy # Module #
# import math # Built In Module #
# This is a comment.
"""This is
Multiline comment"""
# print("Hello World")
# print(math.gcd(2,4)) # Using Math Module
# print("5 + 8") # This is a String.
# print(5 + 8)
# age = 15
# if (age<18):
# print("Hello")
# Variables #
a = 44 # int Variable
b = "Apple" # String Variable
c = 45.82 # Float Variable
# Arithmetic Calculations #
# print(a + c)
# print(a - c)
# print(a * c)
# print(a / c)
# print(a % c) # Modulus
# print(a // c) # Floor Division
# print(a ** c) # Exponent
# Wrong Syntax To Create Variable
# Ex. HCL Project = 45
"""
Rules For Creating Variable
1)Variable should start with a letter or underscore
2)Variable should not start with a number
3)It should not contain space between two letters
4)It should contain only AlphaNumeric characters
5)Variables names are case sensitive
Ex. A and a are two different variables
"""
# Type Of Variables #
TypeA = type(a)
# print(TypeA)
TypeB = type(b)
# print(TypeB)
TypeC = type(c)
# print(TypeC)
x = 44 # int
y = "44" # String
# print(x + 2)
# print(y + 2) # ERROR #
# Type Casting #
w = "22"
w = int(w) # String is converted into integer with the help of Type Casting #
# print(type(w))
# print(w)
v = "22tt"
# v = int(v) # Type Casting is not possible #
# print(v) # ERROR #
a = str(245)
b = float(245)
c = int("24")
d = float("11")
# String Manupulation #
name1 = "Ashish"
name2 = 'Ashish' # name1 and name2 are same
# name3 = "Python
# In One Blog" # ERROR #
name4 = """Python
In One Blog""" # Multiline String
# print(name4)
# In Programming Index starts From 0 #
# print(name1[0]) # It will print character at 0th Index,It means First character
# print(name1[1]) # It will print character at 1st Index,It means Second character
# print(name1[8]) # ERROR - Index out of range
# String Slicing #
# print(name1[:4]) # It will print the characters from 0th index to 3rd index[4th index is excluded]
# print(name1[2:5]) # From 2nd index to 4th index
# print(name1[:]) # From 0th index to last index
# name = " Ashish "
# print(name)
# print(name.strip()) # It will remove all the spaces from the string
# print(len(name)) # It will Print number of elements or characters present in string
# print(name.lower()) # Converts the characters of string into lower case
# print(name.upper()) # Converts the characters of string into upper case
# print(name.replace("As","as")) # Replaces "As" With "as"
str1 = "Python"
str2 = " In One Blog"
# print(str1 + str2) #Concatenate
name5 = "Sachin"
name6 = "Boy"
# print("This is %s and he is a good %s",%(name5,name6)) # Using Format Specifier
# print("This is a {} and he is Good {}".format(name5,name6)) # Format Method
# print(f"This is a {name5} and he is a Good {name6}") # F-Strings
'''
Python Collections:Data Structures
1)List
2)Tuple
3)Dictionary
4)Set
'''
# List #
list1 = [1, 2, 3, 4, 5, 6]
var = type(list1)
# print(list1)
var1 = list1[0] # List slicing similar to string
var2 = list1[1:4]
# print(var1)
# print(var2)
# print(var)
# List is Mutable It means you can change the items of list #
list1[2] = 8
# print(list1)
var3 = len(list1)
# print(var3)
list1.append(99) # It will add the element at the end of list
# print(list1)
list1.insert(2, 3) # Inserts the element in the list
# print(list1)
list1.remove(8) # Delete the element from the list
# print(list1)
list1.pop()
# print(list1)
# del list1 # Deletes List
list1.clear() # Clears The List
# print(list1)
''' For More Information Visit Python Documentation'''
# Tuples #
tup = ("Sachin", "Dhoni", "Rohit", "Virat")
var = type(tup)
# print(tup)
# print(var)
# print(tup[1]) # Returns Second Element
# Tuple is mutable it means you cannot change the items of Tuples
# tup[2] = "Sehwag" # ERROR
# Set #
# Collections of well defined objects
s1 = {1, 2, 2, 3, 3, 4, 4, 5, 5, 5, 5, 6}
s1.add(44)
s1.update([12, 55, 8, 98, 99])
# print(len(s1))
# s1.remove(4444) # ERROR #
s1.discard(4444) # No ERROR [Most preferable to use]
# Like list you can use - .pop,.clear,del
# You can also access Functions like intersection,issubset,union,etc using python documentation
# print(s1)
# Dictionary #
# Key Value Pairs
dict = {"Virat": 100, "Dhoni": 150, "Rohit": 200, "Sachin": 500}
# print(dict.keys()) # Returns keys of dict
# print(dict.values()) # Returns values of dict
# print(dict.items()) # Returns items of dict
# print(dict["Dhoni"]) # Returns runs of Dhoni
# You can try - del,.clear,.pop,.update,.remove,.get,etc using python documenation
# Conditions #
# age = int(input("Enter Your Age\n")) # User Input # \n = new line character
# if age > 50 :
# print("Age is greater than 50")
# elif 15 <= age <= 50 : # elif = else if #
# print("Age is between 15 and 50")
# else :
# print("Age is less than 15")
# Loops #
# Suppose you have to print numbers from 1 to 100
# Using for loop #
# for i in range(1,101): # Range Function = range(0,n-1)
# print(i)
# list = [1,2,3,4,5,6]
# for item in list:
# print(item)
# dict = {"Virat":100,"Dhoni":150,"Rohit":200,"Sachin":500}
# for item in dict:
# print(item)
# s1 = {1,2,2,3,3,4,4,5,5,5,5,6}
# for item in s1:
# print(item)
# tup = ("Sachin","Dhoni","Rohit","Virat")
# for item in tup:
# print(item)
# Using while loop #
# i = 1
# while i < 101:
# print(i)
# i = i + 1
# break statement #
# i = 1
# while i < 101:
# print(i)
# if i == 88:
# break # It will stop the loop
# i = i + 1
# for i in range(1,101):
# print(i)
# if i == 88:
# break
# continue statement #
# i = 1
# while (i<101):
# i = i + 1
# if i == 88:
# continue
# print(i)
# for i in range(1,101):
# if i == 88:
# continue
# print(i)
# Functions #
# def average(a,b): # a,b are arguments #
# return (a + b)/2
# print(average(4,5))
# def average(a = 9,b = 8): # Default Arguments #
# return (a + b)/2
# print(average())
# def average(a,b = 8,c): # ERROR - Non default arguments should not follow default arguments #
# pass # Non executable statement
# OBJECT ORIENTED PROGRAMMING #
# class = Template,Blueprint
# object = instance of class
class Student:
# Class Variables - Accessible by class and also by any object. We can change them with the help of class only,not by the objects #
Year = "SE"
Branch = "Computer Engineering"
def __init__(self, name, year, branch, rollno): # Constructer - Use To Initialized Objects #
self.Name = name
self.Year = year
self.Branch = branch
self.RollNo = rollno
def printdetails(self): # Method #
# ' Self ' refers to the object with the help of which the method is called #
return self.Name
return self.Year
return self.Branch
return self.RollNo
Ashish = Student("Ashish", "SE", "Computer Engineering", 240) # First Object
Rohit = Student("Rohit", "SE", "Computer Engineering", 245) # Second Object
# Instance Variables - Accessible only by objects and not class. We can change them with the help of objects #
Ashish.Name = "Ashish"
Ashish.RollNo = 240
Rohit.Name = "Rohit"
Rohit.RollNo = 245
print(Ashish.Name)
print(Ashish.Year)
print(Ashish.Branch)
print(Ashish.RollNo)
print("\n")
print(Rohit.Name)
print(Rohit.Year)
print(Rohit.Branch)
print(Rohit.RollNo)
print("\n")
print(Ashish.__dict__) # Returns no. of variables defined with the help of objects #
print("\n")
print(Student.__dict__) # Returns no. of variables defined with the help of class or the variables which are defined in the class #
Comments
Post a Comment