Are immutable! a_string_quotes = "Python is love!" a_string_triple = '''Python is life!''' a_list = [1, [2], 1, a_string, 3.67] # Any size, any data type a_tuple = (5, ['z'], 89, ) # Same rules of lists apply, but this are immutable a_tuple_comma = 5, ['z'], 89, # Comma is the element separator a_set = set([1, 1, 4, 1]) # =[1,4] Collection with no duplicate elements @henocdz
be any immutable object # (i.e, numbers, strings, tuples) 'some_key': 'some value', 4: 'another value', a_tuple: a_list # Can be a key only if the tuple contains immutable objects } @henocdz
10 == 10 True # Evaluates if two elements are not equal >>> 10 != 5 True # Evaulates if the value of left operand is greater than the value of right operand >>> 14 < 2 False # Viceversa >>> 14 > 2 True # >= and <= are also supported @henocdz
break: terminates the loop 2. con)nue: ignore the rest of the block and returns to evaluate the expression >>> while True: ... if im_tired: ... break @henocdz
You!') >>> def function_with_params(a_param): ... print(a_param) >>> def function_default_values(a_param, has_default=True): ... """Params with no default value must be defined first""" ... print(a_param, has_default) >>> def give_me_the_year(): ... return 2015 @henocdz
or variables, instead you must use snake case i.e, def thiIsWrong(): def this_is_ok(): 1. PEP8 a. h)ps:/ /www.python.org/dev/peps/pep-0008/ 2. Google Style Guide b. h)p:/ /google.github.io/styleguide/pyguide.html @henocdz
two: a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89] b = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13] Then write a program that returns a list that contains only the elements that are common between the lists (without duplicates). Make sure your program works on two lists of different sizes. @henocdz
the “cows and bulls” game with the user. The game works like this: Randomly generate a 4-digit number. Ask the user to guess a 4-digit number. For every digit that the user guessed correctly in the correct place, they have a “cow”. For every digit the user guessed correctly in the wrong place is a “bull.” Every Ame the user makes a guess, tell them how many “cows” and “bulls” they have. Once the user guesses the correct number, the game is over. Keep track of the number of guesses the user makes throughout teh game and tell the user at the end. Say the number generated by the computer is 1038. An example interacAon could look like this: Welcome to the Cows and Bulls Game! Enter a number: 1234 2 cows, 0 bulls 1256 1 cow, 1 bull Un#l the user guesses the number. @henocdz
# File name: other_file.py >>> import my_math >>> print(my_math.zum(3, 4)) 7 # Just one definition with >>> from my_math import zum >>> print(zum(1, 2)) 2 # Single definition with an alias >>> from my_math import zum as my_sum >>> print(my_zum(12, 12)) 24 @henocdz
to the interpreter, this is executed as script file, so everything in the module file will be executed as-is. If you don't want that, we can use global variable __name__ for code that should be executed only in script mode; like this: if __name__ = '__main__': print('Running as script file...') Global variable __name__ contains the name of the module, but if the module is executed as an script, this variable contains the string __main__ @henocdz
package like this: # import definition >>> from utils.my_math import zum >>> zum(a, b) # import module >>> from utils import my_math >>> my_math.zum() @henocdz
print('I\'m the class `B :)') super(B, self).some_method() # Basic Inheritance is from left to right class C(A, B): pass Super-pro algorithm for inheritance: h4ps://www.python.org/download/releases/2.3/mro/ @henocdz
combined with other object types # Simple way "I like to {}".format('code') # Simple way 2 "I {1} to {0}".format('code', 'like') # 'Complex' way "I {verb} to {what}".format(what='code', verb='like') @henocdz
applica3ons and both depends on MyPackage, but the first one requires version 1 of MyPackage and the later one requires version 2 of MyPackage. The solu)on for that is a virtual environment. In Python 3 you can do so with this command: # pyvenv command it's installed when you install Python3 $ pyvenv ENV_DIR ENV_DIR is the folder where our virtualenv will be installed. @henocdz
must install virtualenv package in order to work with Virtual Environments and the create it, like this: # Install virtualenv package $ pip install virtualenv # Create a virtualenv $ virtualenv ENV_DIR @henocdz