Skip to content

Instantly share code, notes, and snippets.

@pknowledge
pknowledge / python_break_and_continue.py
Created September 7, 2018 01:10
Python Tutorial for Beginners - break and continue
# Python Tutorial for Beginners - break and continue
a = [0, 1, 2, 3, 4, 5]
print('-----------break for loop---------------------')
for x in a:
if x == 2:
break
print(x)
print('-----------continue for loop---------------------')
for x in a:
@pknowledge
pknowledge / Python_Functions.py
Last active September 8, 2018 20:36
Python Functions
# Python Tutorial for Beginners - Functions
def sum(arg1, arg2):
if type(arg1) != type(arg2):
print("Please give the args of same type")
return
return (arg1 + arg2)
a = sum(15, 60)
print(a)
@pknowledge
pknowledge / python_Default_Arguments.py
Created September 10, 2018 17:18
Default Arguments *args and **kwargs in Python
# Python Tutorial for Beginners - Default Arguments *args and **kwargs in Python
print('----Default Arguments----')
def studentExample1(name='unknown', age=0):
print("name: ", name)
print('age', age)
studentExample1()
studentExample1('tom')
studentExample1('tom', 22)
@pknowledge
pknowledge / rectangle.py
Created September 12, 2018 20:57
python Class example
class Rectangle:
pass
rect1 = Rectangle()
rect2 = Rectangle()
rect1.height = 20
rect2.height = 30
@pknowledge
pknowledge / car.py
Created September 12, 2018 20:57
python Class example
class Car:
pass
ford = Car()
honda = Car()
audi = Car()
ford.speed = 200
honda.speed = 220
audi.speed = 250
@pknowledge
pknowledge / __init__and_self_in_python_car.py
Created September 13, 2018 19:21
__init__ and self in python
class Car:
def __init__(self, speed, color):
print(speed)
print(color)
self.speed = speed
self.color = color
print('the __init__ is called')
ford = Car(200, 'red')
honda = Car(250, 'blue')
@pknowledge
pknowledge / __init__and_self_in_python_rectangle.py
Last active February 14, 2022 01:21
__init__and_self_in_python_rectangle.py
class Rectangle:
def __init__(self, height, width):
self.height = height
self.width = width
rect1 = Rectangle(20, 60)
rect2 = Rectangle(50, 40)
@pknowledge
pknowledge / rectangle_Encapsulation.py
Created September 13, 2018 22:19
rectangle_Encapsulation.py
class Rectangle:
def __init__(self, height, width):
self.__height = height
self.__width = width
def set_height(self, height):
self.__height = height
def get_height(self):
return self.__height
def set_width(self, width):
self.__width = width
@pknowledge
pknowledge / car_Encapsulation.py
Created September 13, 2018 22:20
car_Encapsulation.py
class Car:
def __init__(self, speed, color):
self.__speed = speed
self.__color = color
def set_speed(self, value):
self.__speed = value
def get_speed(self):
return self.__speed
@pknowledge
pknowledge / public_and_private_members.py
Last active September 15, 2018 19:45
public_and_private_members.py
class Hello:
def __init__(self, name):
self.public_variable = 10
self.__private_variable = 30
def public_method(self):
print(self.public_variable)
print(self.__private_variable)
print('public')
self.__private_method()