Quick overview of the syntax of class in python. Simple form: class NameOfClass: attributes . . . More wholesome form: class ClassExample: """Documentation part """ attribute = "Class Variable" # class varirable which is shared by all its instances def function(self): # self == object/instance return "Function called" def __init__(self, instance_var): # Constructor like. No return statement. Only initialization self.inst_var = instance_var # unique to each instance obj = ClassExample("Instance Variable") print( obj.attribute, "\n"+obj.inst_var, "\n"+obj.function(), "\n"+obj.__doc__ ) OUTPUT Class Variable Instance Variable Function called Documentation part Inheritance concept is also included in Python. class Base: some_list = [] # common ...
Read more
String Concatenation using ‘+’ upper() lower() split() str() len() format() Substitution and Date Time str1 = "Some text" str2 = " Some other text" concatenate_str = "Concatenation: " + str1 + str2 print(concatenate_str) format_str = "firs the tab \t then the new line \nthis format takes effect only after using in \'print\' \ inline break which isn't \"line break\" ".lower() cap_str = "\ncapitalized".upper() print(format_str) print(cap_str) print("-----------------") some_text = "Splitting to get words, or with comma" print(some_text.split()) print(some_text.split(",")) print("Length of some_text: " + str(len(some_text))) print("\n-----------------") print("SUBSTITUTION\n") subst_text = "With {variable} substitution.".format(variable="variable") print(subst_text) subst_text = "With {0} {1}.".format("positional", "substitution") print(subst_text) subst_text = ...
Read more
Function is basically used for making code re-usable. The keyword in python to define function is ‘def’. def function_name( parameters ): #body of function print "Hello Wolrd" return (some-expression-or-variable) All parameters in Python are pass by value i.e. modifying parameter value inside function will reflect the changes in variable outside the function. Let’s learn more about python functions through code. def variable_change_test( some_var, some_list ): some_var = 21; #NOTE: We are creating a new local variable using assignment operator some_list.append(4) print("some_var inside function: ", some_var) print("some_list inside function: ", some_list) return some_var = 11 some_list = [1,2,3] variable_change_test(some_var, some_list) print("-------------------------") ...
Read more
Tuples are very much like lists but the difference is that tuples cannot be changed. Tuples are denoted by: () whereas lists are denoted by: [] To access element in tuple we make use of square brackets. E.g. tup[0] Tuples have similar functions as list as they are: cmp(tup1, tup2) len(tup) max(tup) min(tup) tuple(sequence) Let’s see tuples in action: >>> tup = () >>> tup = ("xyz", "xyz") >>> tup ('xyz', 'xyz') >>> tup = ( ... ("one", "one"), ... ("two", "two"), ... ) >>> tup (('one', 'one'), ('two', 'two')) >>> tup[0] ('one', 'one') >>> tup[0][0] 'one' >>> tup += ...
Read more
Dictionary in Python is represented with key value pairs. Keys must be unique whereas values are not. Let’s understand some basics by example: >>> a_dict = {} >>> a_dict = { "abc" : "String 1" } >>> a_dict {'abc': 'String 1'} >>> a_dict["abc"] = "String Change" >>> a_dict {'abc': 'String Change'} >>> a_dict["abc"] 'String Change' >>> a_dict[0] = "New dictionary item where key is integer" >>> a_dict {'abc': 'String Change', 0: 'New dictionary item where key is integer'} >>> a_list = [1,2,3] >>> a_dict['some_list'] = a_list >>> a_dict {'abc': 'String Change', 0: 'New dictionary item where key is integer', 'some_list': ...
Read more
List in Python is like ArrayList in Java. The elements are addressed by index which starts from 0. The list may contain elements of different data types unlike arrays in C or C++. Python has lots of in-built functionalities when it comes to list: cmp(list1, list2) len(list) max(list) min(list) list(sequence) – tuples are converted into a list Let’s see rest of the functions of list in python in action. Note: This is Command Line Python >>> a_list = [] >>> a_list [] >>> a_list = ["something",234] >>> a_list ['something', 234] >>> a_list[0] 'something' >>> a_list.count(234) 1 >>> a_list.append(334) >>> a_list ...
Read more