print(dir())
print(help(print))
# intergal
num = 3
print(type(num))
# Float with decimal
num = 3.14
print(type(num))
# Calculation
print(3 + 2) # Plus
print(3 - 2) # Minus
print(3 * 2) # Multiply
print(3 / 2) # Division
print(3 // 2) # Floor Division
print(3 ** 2) # Exponents
print(3 % 2) # Modulus
print(3 * (2 + 1))
# Calculation
num = 1
num = num + 1
print(num)
num += 2
print(num)
print(abs(-3))
print(round(4.555))
print(round(4.555,1))
# Operation
a = 3
b = 2
print(a == b)
print(a != b)
print(a > b)
print(a < b)
print(a >= b)
print(a <= b)
a = '100'
b = '200'
print(a+b)
type(a)
a = '100'
b = '200'
a = int(a)
b = int(b)
print(a+b)
type(a)
# Create String
# String are built either single, double or triple quotes
my_text1 = 'This is a string'
my_text2 = "This is a string"
my_text3 = '''This is a multi-line strings.'''
my_text4 = r"This is a string \t test" # string is interpreted as it is without interpreting the special character
my_text5 = "This is a string \t test"
# Slice strings
my_text1 = 'This is a string'
print(my_text1[0]) # return scharacter from index
print(my_text1[-1]) # return last character
print(my_text1[2:4]) # return character from start to end index
# Formatter
print("Value is %s" % "A") # % produce formated value
print("Value is %s %s" % ("A", "B")) # % proudct two formated values
print("Value is {a} and {b}". format(a=1, b =2)) # {} a string with argument
# Operators
my_text1 = 'This is a string'
my_text1 + "1" # cat1
print(my_text1)
my_test2 = my_text1 * 2 # catcat
print(my_test2)
print(my_text1.count('i')) # countspecific charactor
len(my_text1) # count all charactors
# Check type
print("1".isdigit()) # Is the string made of digit only?
print("A".isalpha()) # Is the string made of alphabet only?
print("A".isupper()) # Is the string made of upper case only?
print("a".islower()) # Is the string made of lower case only?
print("Is".istitle()) # Is the string start with capital letter?
# modifiy string
my_text1 = 'This is a TEST String'
print(my_text1.title()) # 'This Is A Test String'
print(my_text1.capitalize()) # 'This is a test string'
print(my_text1.upper()) # 'THIS IS A TEST STRING'
print(my_text1.lower()) # 'this is a test string'
print(my_text1.swapcase()) # 'tHIS IS A test sTRING'
print(my_text1.center(30)) # ' This is a TEST String '
# modifiy string
my_text1 = 'This is a TEST String'
print(my_text1.ljust(30)) # 'This is a TEST String '
print(my_text1.rjust(30)) # ' This is a TEST String'
print(my_text1.rjust(30, '-')) # '---------This is a TEST String'
print(my_text1.zfill(30)) # '000000000This is a TEST String'
# modifiy string
my_text1 = " This is a TEST String "
print(my_text1.strip()) # 'This Is A Test String'
print(my_text1.rstrip()) # ' This Is A Test String'
print(my_text1.lstrip()) # 'This Is A Test String '
print(my_text1.lstrip()) # 'This Is A Test String'
# replace() 'This is a my String'
my_text1 = "This is a TEST String"
my_text2 = my_text1.replace('TEST', 'my')
my_text2
# partion() ('Th', 'is', ' is a TEST String')
my_text1 = "This is a TEST String"
my_text2 = my_text1.partition('is')
my_text2
# rpartion() ('This ', 'is', ' a TEST String')
my_text1 = "This is a TEST String"
my_text2 = my_text1.rpartition('is')
my_text2
# split() ['This', 'is', 'a', 'TEST', 'String']
my_text1 = "This is a TEST String"
my_text2 = my_text1.split(' ')
my_text2
# Join()
' '.join(['Good', 'Night']) # 'Good Night'
# endswith() may provide optional start and end indices
my_text1 = "This is a TEST String"
my_text1.endswith('ing')
# startswith() may provide start and end indices
my_text1 = "This is a TEST String"
my_text1.startswith('This')
# find()
my_text1 = "This is a TEST String"
print(my_text1.find('is')) # returns start index of 'is' first occurence
print(my_text1.find('is', 4)) # starting at index 4, returns start index of 'is' first occurence
print(my_text1.rfind('is')) # returns start index of 'is' last occurence
print(my_text1.index('is')) # like find but raises error when substring is not found
print(my_text1.rindex('is')) # like rfind but raises error when substring is not found
# Create list
my_list1 = [1, 2, 3, 4, 5]
my_list2 = ['one', 'two', 'three', 'two']
my_list3 = [1, 'two', 'three']
my_list4 = list(range(1,10))
my_list5 = [1, 3 ,2, 4]
my_list6 = [1, 2 ,3, 4]
my_list7 = [1, 2 ,[3, 4]] # nested list
# Copy
import copy
my_list1 = [1, 2, 3, 4, 5]
copied_list1 = copy.copy(my_list1) # shallow copy - nested objects will not be copied
copied_list1 = copy.deepcopy(my_list1) # deep copy - nested objects will be copied
# Search element
my_list1 = ['one', 'two', 'three', 'two']
print(my_list1.index('two')) # index() returns the index of the first occurence.
print(my_list1.index('two', 2)) # index() returns the index of the second occurence.
# Slicing
my_list1 = [1, 2, 3, 4, 5]
print(my_list1[1]) # [] return an object from index
print(my_list1[:]) # return all objects in list
print(my_list1[1:]) # return index object to last object
print(my_list1[:1]) # return first object to index
print(my_list1[1:2]) # return index object to index
# edit: append() appends object to the end of list
my_list1 = [1, 2, 3, 4, 5]
my_list1.append(6)
my_list1
# edit: extend() append list to the end of list
my_list1 = [1, 2, 3, 4, 5]
my_list2 = [5, 6]
my_list1.extend(my_list2)
my_list1
# edit: insert() insert an object before the index provided
my_list1 = [1, 2, 3, 4, 5]
my_list1.insert(2, 'four')
my_list1
# edit: remove() remove the first occurence of an element
my_list1 = [1, 2, 3, 4, 5]
my_list1.remove(3)
my_list1
# edit: pop() remove the last element of a list
my_list1 = [1, 2, 3, 4, 5]
my_list1.pop()
my_list1
# edit: sort() performs an in-place ascending sorting
my_list1 = [1, 3, 2, 2, 5]
my_list1.sort()
my_list1
# edit: sort() performs an in-place descending sorting
my_list1 = [1, 3, 2, 2, 5]
my_list1.sort(reverse=True)
my_list1
# edit: reverse() reverse the element in-place
my_list1 = [1, 3, 2, 2, 5]
my_list1.reverse()
my_list1
# calculation
my_list1 = [1, 3, 2, 2, 5]
print(my_list1.count(2)) # count() count the number of element
print(len(my_list1)) # len() count all elements in list
my_list1 += [1] # append object to the end of list
print(my_list1)
my_list2 = my_list1 * 2 # extend list 2 times
print(my_list2)
my_list2 = [x + 1 for x in my_list1] # add 1 in all elements in list
print(my_list2)
# Calculation
list1 = [1, 5, 6, 3, 2, 1]
print(min(list1))
print(max(list1))
print(sum(list1))
# Filtering
my_list1 = [1, 3, 2, 2, 5]
my_list2 = [i for i in my_list1 if i > 2] # get element more than 2
print(my_list2)
my_list2 = [i for i in my_list1 if i % 2 == 0] # get event number list
print(my_list2)
# Condition
list1 = ['a', 'b', 'c','d']
print('b' in list1)
# Condtion
list1 = ['a', 'b', 'c','d']
for item in list1:
print(item)
# Condition
list1 = ['a', 'b', 'c','d']
for index, item in enumerate(list1, start=1):
print(index, item)
my_tuple1 = (1, 'two', 'three')
my_tuple2 = 1, 'two'
# Change data type
list1 = ['a', 'b', 'c','d']
tuple1 = tuple(list1)
# Create Dictionary
my_dict0 = {} # create blank dictionary
my_dict1 = {'a':1, 'b':2, 'c': 'three', 'c': 'three'}
my_dict2 = {}.fromkeys(['a', 'b', 'c']) # create dictionary key
my_dict3 = {'a':1, 'b':2}
my_dict4 = {('x1', 'y1'): 1,
('x1', 'y2'):2}
# print
dic1 = {'name':'Tom','age':25, 'courses':['Match','CompSci']}
print(dic1)
print(dic1.keys())
print(dic1.values())
print(dic1.items())
# print
dic1 = {'name':'Tom','age':25, 'courses':['Match','CompSci']}
for key in dic1:
print(key)
# print
dic1 = {'name':'Tom','age':25, 'courses':['Match','CompSci']}
for key, value in dic1.items():
print(key, value)
# access value
my_dict1 = {'a':1, 'b':2, 'c': 'three', 'c': 'three'}
print(my_dict1['a']) # [] returns value from key
print(my_dict1.get('d', 0)) # get() returns value from key or get an optional value, if the key is not found
print(my_dict1.items()) # items() returns a list of items of the form (key, value)
# edit: pop() return value and removes the corresponding value
my_dict1 = {'a':1, 'b':2, 'c': 'three', 'c': 'three'}
my_dict1.pop('a')
my_dict1
# edit: popitem() return value and removes the corresponding pair
my_dict1 = {'a':1, 'b':2, 'c': 'three', 'c': 'three'}
my_dict2 = my_dict1.popitem()
my_dict2
# edit: copy() copy dictionary (shallow copy)
my_dict1 = {'a':1, 'b':2, 'c': 'three', 'c': 'three'}
my_dict2 = my_dict1.copy()
my_dict2
# edit: clear() Remove all its items
my_dict1 = {'a':1, 'b':2, 'c': 'three', 'c': 'three'}
my_dict1.clear()
my_dict1
# edit: clear() Remove all its items
my_dict1 = {'a':1, 'b':2, 'c': 'three', 'c': 'three'}
my_dict1.clear()
my_dict1
# edit: del removes the corresponding item
my_dict1 = {'a':1, 'b':2, 'c': 'three', 'c': 'three'}
del my_dict1['b']
my_dict1
# edit: update() update value from another dictionary
my_dict1 = {'a':1, 'b':2, 'c': 'three', 'c': 'three'}
my_dict2 = {'a':7}
my_dict1.update(my_dict2)
my_dict1
# edit: assign value by key
my_dict1 = {'a':1, 'b':2, 'c': 'three', 'c': 'three'}
my_dict1['c'] = 7
my_dict1
# edit: assign value by list key
my_dict1 = {('x1', 'y1'):1, ('x1', 'y2'):2}
my_dict1[('x1', 'y1')] = 7
my_dict1
# Create Sets
# Sets are mutable unordered sequence of unique elements.
my_set1 = set([1, 2, 3])
my_set2 = set([3, 4, 5])
# Union
my_set1 = set([1, 2, 3])
my_set2 = set([3, 4, 5])
my_set1 | my_set2
my_set1.union(my_set2)
# Intersection
my_set1 = set([1, 2, 3])
my_set2 = set([3, 4, 5])
my_set1 & my_set2
my_set1.intersection(my_set2)
# Subset
my_set1 = set([1, 2, 3])
my_set2 = set([3, 4, 5])
my_set1 < my_set2
my_set1.issubset(my_set2)
# Difference
my_set1 = set([1, 2, 3])
my_set2 = set([3, 4, 5])
my_set1 - my_set2
my_set1.difference(my_set2)
# Symmetric Difference
my_set1 = set([1, 2, 3])
my_set2 = set([3, 4, 5])
my_set1 ^ my_set2
my_set1.symmetric_difference(my_set2)
# frozensets
# frozensets are immutable unordered sequence of unique elements.
my_frozenset1 = frozenset([1, 2, 3])
my_dict = {frozenset([1,2]): 1} # use a set as a key dictionary
# if
language = 'Python'
if language == 'Python':
print('Condition was True')
# if else
language = 'Python'
if language == 'Python':
print('Condition was True')
else:
print('Not match')
# if elif else
language = 'Java'
if language == 'Python':
print('It is Python')
elif language == 'Java':
print('It is Java')
else:
print('Not match')
# if and
user ='Admin'
logged_in = True
if user == 'Admin' and logged_in:
print('Admin page')
else:
print('Bad creditial')
# if or
user ='Admin'
logged_in = False
if user == 'Admin' or logged_in:
print('Admin page')
else:
print('Bad creditial')
# if not
user ='Admin'
logged_in = False
if not logged_in:
print('Please log in')
else:
print('Welcome')
# False
condition = False
if condition:
print('Evaluated to True')
else:
print('Evaluated to False')
# None = False
condition = None
if condition:
print('Evaluated to True')
else:
print('Evaluated to False')
# 0 = False
condition = 0
if condition:
print('Evaluated to True')
else:
print('Evaluated to False')
# Blank = False
condition = ''
if condition:
print('Evaluated to True')
else:
print('Evaluated to False')
# for in
muns = [1, 2, 3, 4, 5]
for num in muns:
print(num)
# for in if
muns = [1, 2, 3, 4, 5]
for num in muns:
if num == 3:
print('Found!')
print(num)
# for in if break
muns = [1, 2, 3, 4, 5]
for num in muns:
if num == 3:
print('Found!')
break
print(num)
# for in if continue
muns = [1, 2, 3, 4, 5]
for num in muns:
if num == 3:
print('Found!')
continue
print(num)
# for in for in
muns = [1, 2, 3, 4]
for num in muns:
for letter in 'abc':
print(num, letter)
# for in range
for i in range(5):
print(i)
# for in range
for i in range(1, 6):
print(i)
# while
x = 0
while x < 5:
print(x)
x +=1
# while if
x = 0
while x < 5:
if x == 3:
break
print(x)
x +=1
# while True
x = 0
while True:
if x == 3:
break
print(x)
x +=1
# def fuction() pass
def hello_func():
pass
hello_func()
# print(function)
def hello_func():
pass
print(hello_func)
print(hello_func())
# print(function())
def hello_func():
print('Hello Function!')
hello_func()
# def fuction() return
def hello_func():
return 'Hello Function!'
hello_func()
# def fuction() return
def hello_func():
return 'Hello Function!'
print(hello_func())
# def fuction() return
def hello_func():
return 'Hello Function!'
print(hello_func().upper())
# def function(parameter)
def hello_func(greeting):
return '{} Function.'.format(greeting)
hello_func('Hi')
# def function(arg)
def hello_func(greeting, name = 'John'):
return '{},{} Function.'.format(greeting, name)
print(hello_func('Hi'))
def hello_func(greeting, name = 'You'):
return '{}, {} '.format(greeting, name)
print(hello_func('Hi', 'Tom'))