Scarica Appunti PHYTON per beginners e più Appunti in PDF di Marketing solo su Docsity! SOCIAL MEDIA MARKETING PYHTON PARENTESI QUADRATA = OPTION + [] PARENTESI GRAFFA = OPTION SHIFT + {} OBJECTS 1. INTEGERS Numbers 2. STRINGS Series of alphanumeric characters 3. LIST Ordered collection of objects - ORDERED: The position (INDEX) of the elements in the list matters because each element is indicated with an index. Through this index we can retrieve elements in the list through the command: name_list(index) Index of the first element: 0 - NON-UNIQUE: In lists we can have the same element appearing more than once 4. SETS Unordered collection of unique objects - UNORDERED: elements in a set have no index and so we cannot retrieve single element froma set. - UNIQUE: if we have duplicates in a set, the set will disregard them (we cannot have duplicate elements) 5. TUPLES Immutable list of objects - IMMUTABLE: we cannot change the elements of a tuple 6. DICTIONARIES Associate unique keys to specific values INTEGER name_integer=value CREATE integer "num" whose value is 3 num=3 print(num) STRINGS name_string="value” CREATE a string whose value is “cheese” and whose name is “food” food="cheese" print(food) Print the sentences: - Ryan is 23. He likes cheese. name="Ryan" age=23 print(name+" is "+str(age)+". He likes "+food) OR print(name,”is”,age,”. He likes”,food) ALPHANUMERIC CHARACTERS NEED ‘“...” LIST name list=[“a”,"b”,"c"] made by strings name_list=[1,2,3] made by integers name_list=[a,b,c] made by variables . CREATE a list of STRINGS called “greenday”, which contains the name of the members greenday=/"Billie”,” Tre Cool”,” Mike” ] print(greenday) . CREATE a list of INTEGERS primes=/1,3,5,7] print(primes) . CREATE a list of VARIABLES singer=" Billie” drummer=" Tre Cool” bass=" Mike” band=/singer,drummer, bass] print(band) List1=/1,3,5,7) Select the: - Firstelement print(grades) . CREATE a tuple of DIFFERENT TYPES of objects student_grades=(“Kirs”,23), (“Paul”,30), (“Mark”,22) RETRIVE the first element of the tuple “grades” print(grades/0)) . CREATE a list from two tuple zip(students,grades) students2=list(zip(students,grades)) print(students2) DICTIONARIES name dictionary= fl key1:value1, key2:value2 # Keys can be strings, integers or tuples but no lists . CREATE from SCRATCH grades= }“Kirs”:30, “Paul”:23, “Mark” :25 } Print(grades) . CREATE from LIST names=(“Kirs”,” Paul,” Mark”) grades=(30, 23,25) grades2=dict(zip(names_key,grades_value)) RETRIVE - Paul's grade print(grades/” Paul” )) print(name_dictionary[name_key]) - All the values print(grades.values()) - All the keys print(grades.keys()) If we try to retrive a key and it does not exist print(grades/” Abbie” )) KeyError: “Abbie” CHANGE the value of a dictionary grades/” Mark” F.26 ADD a new item to a dictionary grades/” Abbie” F20 print(grades/' Abbie” )) MULTILEVEL DICTIONARIES Dictionaries where keys are dictionaries themselves name_dictionary= fkey1: fkey1.1:value1.1, key2.1:value2.1 6 key2: fkey2.1:value2.1, key2.2:value2.2 {} CREATE a multilevel dictionary students= 1“Kirs”:{”grade”: 30,"skill’: “math” } “Paul”: {” grade”: 23,’ skill”: “latin” È, “Mark”:1”grade”: 25,’ skill’: “Phyton” #} RETRIVE: - allthe informations about Paul print(students[” Paul”]) - Paul's best skill print(students[” Paul”]["skill"]) FOR LOOP Is a mechanism for which computer repeats the same block of codes multiple times For <iterator> in <iterator>: <instructions> TRADITIONAL METHOD: aaa=2 print(element1=aaa+1) 3 aaa=3 print(element1=aaa+1) 4 aaa=4 print(element1=aaa+1) 5 FOR LOOP METHOD: 1 list1=[2,3,4] For aaa in list1: per tutti i valori nella list1: elementi=aaa+1 sommaci 1 print(element1) 3 4 5 2. range function for aaa in Range(0,3): per tutti i valori compresi tra 0 e 3: Print(aaa) mostrali 0 1 2 oR List1=/2,3,4] For aaa in range(len(list1)): this is the equivalent of range (0,3) -> for aaa in range(3) Print(aaa) 0 1 2 we obtain the indices of list1 (aaa=index of elements in list 1) FUNCTIONS Is a block of resuable code that you can use to perform specific action. Def <name> (<argument>): [xstatement>] return <value> 1. Definition 2. Calling 1. We want to create a function that takes a number as input and it returns the double of that number Def double(n): g=n*2 return g 2. We want to call the function double with number 2 print(double(2)) 4 FUNCTIONS WITH MORE ARGUMENTS We want to define and call a function that multiples two numbers and returns the outcome of this multiplication Def multiplication(a,b): mult=a*b return mult print(multiplication(3,5)) 15 FUNCTIONS PRINT print(input) TYPE OF THE OBJECT print(type(object)) CHANGE VALUE name_object_WantToChange=new_ value name_object/“Changing_element_index” Fnew_value ADD name_object/“New_element” Fvalue DELETE dellist_name/index] SUM elementi+element2 sum(list_.name) OR sum(elementi,element2) LENGTH (number of elements) len(object) BOOLEAN (check if 2 objects are the same) object 1==object2 RETRIEVE (select an element of a list) list_namefindex] - forward methods [0,1,2,...,...] - negative methods /... SLICE (select few elements of a list) list_namefindex_first_INCLUDE:index_first_EXCLUDE] METHOD object.method() - APPEND (adds ONE element at the END of the list) list_name.append(element) - INSERT (adds ONE element at a CERTAIN position of the list) list_name.insert(index,element) - EXTEND (adds SEVERAL elements at the END of the list) list_name.extend(object_ WantToAddAtTheEnd) - COUNT (number of times a certain element/number appears in a list) list_name.count(element) - INDEX (index of the FIRST occurrence of a certain element in a list) list_name.index(element) - UNION (returns ALL the elements in ALL the sets (union of the sets)) set_name.union(set_name2,set_name3,...) - INTERSECTION (Returns the elements in COMMON between sets) set_name.intersection(set_name2,set_name3,...) ZIP (allow to combine 2 lists/dictionaries) zip(list1,list2) list(zip(list1,list2) dict(zip(list1,list2) RANGE (returns a range of numbers, starting from the first number “n” an the last number “m”) range(n,m) up to end but not including CAST (Translate an object into another) Str(object) casts an object to a STRING Set(object) casts an object to a SET List(object) casts an object to a LIST Int(object) casts an object to a INTEGER Float(object) casts an object to a FLOAT Dict(object) casts an object to a DICTIONARY SORTED (returns the values in order -> descending order: REVERSE=TRUE) Sorted(list, reverse=True/False)