Variable
a = 10
c = 10.2
b = False
d = 'A'
e = "Apple"
print(a)
print(b)
print(c)
print(d)
print(e)
Operator & Precedence
result = 10 + 20
print(result)
result = 1 + 1 * 10 + 30
print(result)
result = 1 + 2 * 10 + 30
print(result)
result = (1 + 1) * (10 + 30)
print(result)
Function
def delivery_check(pincode):
print("delivery to ",pincode)
delivery_check("600100")
Main function
def start():
print("Hello from main function")
if __name__ == "__main__":
start()
Class, Object and Method
class Order:
def __init__(self,id,pincode):
self.id = id
self.pincode = pincode
def display(self):
text = "Order : {id}, to : {pincode}"
print(text.format(id = self.id,pincode=self.pincode))
order = Order(101,600100)
order.display()
Inheritence
class NonVegOrder(Order):
def __init__(self,id,pincode):
super().__init__(id,pincode)
order = NonVegOrder(101,600100)
order.display()
String & Array
name = "Test"
char_name = ['p','y','t','h','o','n']
name = ""
new_name = name.join(char_name)
print(new_name)
print(name)
List & Loop
char_name = ['p','y','t','h','o','n']
name = ""
for c in char_name:
name = name + c
print(name)
List
names = list(("Ranjith","Raj"))
print("Names length : ",len(names))
names.append('D')
print(names)
Array
import array as a
digits = a.array("i",[1,2,3])
for d in digits:
print(d)
if & else
check = False
if check :
print("Its true")
else:
print("Its false")
Dictionary
map = {
"java": 100,
"rust" : 200,
"python": 300
}
print(map["java"])
Read input
number = int(input("Enter input"))
if number < 10:
print("Number less than 10")
else:
print("Number greater than or equal to 10")
Import
import time
print(time.ctime(time.time()))
Threading
import threading
def printme(name):
print(name)
thread = threading.Thread(printme("first"))
thread.start()
Multi threading
import threading
import time
class MyThread(threading.Thread):
def __init__(self, name,count,sleep_time):
threading.Thread.__init__(self)
self.name = name
self.count = count
self.sleep_time = sleep_time
def run(self):
for i in range(0, self.count):
print(self.name,i)
time.sleep(self.sleep_time)
thread1 = MyThread("Fast",5,0.5)
thread2 = MyThread("Slow",5,1)
thread1.start()
thread2.start()
print("Main thread done")
Thread join
import threading
import time
class MyThread(threading.Thread):
def __init__(self, name,count,sleep_time):
threading.Thread.__init__(self)
self.name = name
self.count = count
self.sleep_time = sleep_time
def run(self):
for i in range(0, self.count):
print(self.name,i)
time.sleep(self.sleep_time)
thread1 = MyThread("Fast",5,0.5)
thread2 = MyThread("Slow",5,1)
thread1.start()
thread2.start()
thread1.join() # Current Main thread joined to thread1 to complete
thread2.join() # Current Main thread joined to thread2 to complete
print("Main thread done")
Logging
import logging
logger = logging.getLogger('MyApplication')
logger.info('This is to showcase info level logging in python')
level = "Warning"
logger.info('This is to showcase %s level logging in python',level)
JSON
import json
order = '{"id" : 10,"user" : "ranjith"}'
order_json = json.loads(order) # Convert string to json
print(order_json['id'])
print(json.dumps(order_json)) # Convert json to string
No comments:
Post a Comment