PYTHON
PYTHON
if site == 'gfg':
print('Logging on to geeksforgeeks...')
else:
print('retype the URL.')
print('All set !')
3 # An integer assignment
age = 45 45
1456.8
# A floating point John
salary = 1456.8
# A string
name = "John"
print(age)
print(salary)
print(name)
# print results
print(add)
print(sub)
print(mul)
print(div1)
print(div2)
print(mod)
5 # Examples of Relational Operators
a = 13 False
b = 33 True
False
# a > b is False True
print(a > b) False
True
# a < b is True
print(a < b)
# a == b is False
print(a == b)
# a != b is True
print(a != b)
# a >= b is False
print(a >= b)
# a <= b is True
print(a <= b)
# Print a or b is True
print(a or b)
# Identity operator
print(a1 is not b1)
print(a1 is b1)
# Membership operator
print("G" in a1)
print("N" not in b1)
x=5
# Two objects are passed
print("x =", x)
12 # Python program to
# demonstrate numeric value Type of a: <class 'int'>
c = 2 + 4j
print("\nType of c: ", type(c))
# Using append()
List.append(1)
List.append(2)
print(List)
# Using insert()
List.insert(3, 12)
List.insert(0, 'Geeks')
print(List)
# Using extend()
List.extend([8, 'Geeks', 'Always'])
print(List)
# accessing a element
print(List[0])
print(List[2])
# Negative indexing
# print the last element of list
print(List[-1])
# print the third last element of list
print(List[-3])
19 # Python program to demonstrate
# Removal of elements in a List [1, 2, 3, 4, 7, 8, 9, 10, 11, 12]
[1, 2, 3, 4, 7, 8, 9, 10, 11]
# Creating a List
List = [1, 2, 3, 4, 5, 6,
7, 8, 9, 10, 11, 12]
# using pop()
List.pop()
print(List)
20 # Python program to demonstrate
# creation of Set ()
('Geeks', 'For')
# Creating an empty tuple (1, 2, 4, 5, 6)
Tuple1 = () ((0, 1, 2, 3), ('python', 'geek'))
print (Tuple1)
21 # Python program to
# demonstrate accessing tuple 1
5
tuple1 = tuple([1, 2, 3, 4, 5])
23 # Python program to
# demonstrate boolean type <class 'bool'>
False
print(type(True)) True
print(1>2)
print('a'=='a')
set1 = set()
# Creating a set
set1 = set(["Geeks", "For", "Geeks"])
print(Dict)
# Creating a Dictionary
Dict = {1: 'Geeks', 'name': 'For', 3: 'Geeks'}
# using pop()
Dict.pop(5)
print(Dict)
# using popitem()
Dict.popitem()
print(Dict)
a = 10
b = 15
a = 10
# is-elif
if (a == 11):
print ("a is 11")
elif (a == 10):
print ("a is 10")
else:
print ("a is not present")
35 # Python program to illustrate Hello Geek
# while and while-else loop Hello Geek
i=0 Hello Geek
while (i < 3): 4
i=i+1 3
print("Hello Geek") 2
1
# checks if list still 11
# contains any element
a = [1, 2, 3, 4]
while a:
print(a.pop())
i = 10
while i < 12:
i += 1
print(i)
break
else: # Not executed as there is a break
print("No Break")
for i in range(5):
print(i, end =" ")
print()
# incremented by 3
for i in range(15, 25, 3):
print(i, end =" ")
for letter in s:
if letter == 'e' or letter == 's':
break
print(letter, end = " ")
print()
for letter in s:
if letter == 'e' or letter == 's':
continue
print(letter, end = " ")
print()
for letter in s:
if letter == 'e' or letter == 's':
pass
print(letter, end = " ")
# Defining functions
def ask_user():
print("Hello Geeks")
# Calling functions
ask_user()
res = my_func()
print(res)
# Driver code
myFun(10)
# Keyword arguments
student(firstname ='Geeks', lastname ='Practice')
student(lastname ='Practice', firstname ='Geeks')
# Driver code
myFun1('Hello', 'Welcome', 'to', 'GeeksforGeeks')
print()
myFun2(first ='Geeks', mid ='for', last ='Geeks')
# A sample method
def fun(self):
print("I'm a", self.attr1)
print("I'm a", self.attr2)
# Driver code
# Object instantiation
Rodger = Dog()
class Addition:
# parameterized constructor
def __init__(self, f, s):
self.first = f
self.second = s
def calculate(self):
print(self.first + self.second)
# perform Addition
obj.calculate()
# Initializing
def __init__(self):
print('Employee created.')
obj = Employee()
del obj
class Person():
# Constructor
def __init__(self, name):
self.name = name
# To get name
def getName(self):
return self.name
# Calling constructor of
# Base class
Base.__init__(self)
print("Calling private member of base class: ")
print(self.__a)
# Driver code
obj = Derived()
class A():
def show(self):
print("Inside A")
class B():
def show(self):
print("Inside B")
# Driver's code
a = A()
a.show()
b = B()
b.show()
file1.seek(0)
# readlines function
print("Output of Readlines function is ")
print(file1.readlines())
print()
file1.close()
# writing to file
# Opening a file
s = "Hello\n"
file1.write(s)
# at a time
file1.writelines(L)
# Closing file
file1.close()
# Defining a function
def Geeks():
print("GeeksforGeeks")
# Defining a variable
location = "Noida"
# Defining a class
class Employee():
def show(self):
print("Employee name:", self.name)
print("Employee position:", self.position)
def __init__(self):
self.models = ['i8', 'x1', 'x5', 'x6']
def outModels(self):
print('These are the available models for BMW')
for model in self.models:
print('\t % s ' % model)
def __init__(self):
self.models = ['q7', 'a6', 'a8', 'a3']
def outModels(self):
print('These are the available models for Audi')
for model in self.models:
print('\t % s ' % model)
# findall()
import re
regex = '\d+'
print(match)
60 # A Python program to demonstrate working of ['e', 'a', 'd', 'b', 'e', 'a']
# compile()
import re
# it is equivalent to [abcde].
p = re.compile('[a-e]')
if match == None:
print("Not a valid date")
return
# Driver Code
findMonthAndDate("Jun 24")
print("")
findMonthAndDate("I was born on June 24")
if match != None:
else:
print("The regex pattern does not match.")
# working of try()
try:
result = x // y
except ZeroDivisionError:
divide(3, 2)
64 # Python code to illustrate Sorry! You are dividing by zero
# working of try()
def divide(x, y):
try:
result = x // y
print("Yeah ! Your answer is :", result)
except:
print("Sorry ! You are dividing by zero ")