Docsity
Docsity

Prepara i tuoi esami
Prepara i tuoi esami

Studia grazie alle numerose risorse presenti su Docsity


Ottieni i punti per scaricare
Ottieni i punti per scaricare

Guadagna punti aiutando altri studenti oppure acquistali con un piano Premium


Guide e consigli
Guide e consigli


Appunti PHYTON per beginners, Appunti di Marketing

Il documento contiene gli appunti per imparare a muovere i primi passi con PHYTON. Tali appunti sono stati estrapolati dai documenti e dalle lezioni tenute dalla professoressa GAIA RUBERA, nel corso di SOCIAL MEDIA MARKETING presso l'università BOCCONI.

Tipologia: Appunti

2020/2021

Caricato il 05/10/2021

andreabonetti04
andreabonetti04 🇮🇹

2 documenti

1 / 13

Toggle sidebar

Questa pagina non è visibile nell’anteprima

Non perderti parti importanti!

bg1
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
from a 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
pf3
pf4
pf5
pf8
pf9
pfa
pfd

Anteprima parziale del testo

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 from a 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= 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= 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:

  • First element

CREATE a set from the list 1,2,3,3,4,4,5,5,6,7,8,9,0 set1=set( 1,2,3,3,4,4,5,5,6,7,8,9,0 ) print(set1) 0,1,2,3,5,6,7,8,9  -> length=

Convert STRING to a LIST: Name_list=name_string.split()

Convert a LIST of words into a STRING: Name_string=” “.join(name_list)

TUPLES

name_tuple=(“a”,“b”, …) made by strings name_tuple=(1,2, …) made by integers name_tuple=(“a”,1), (“b”,2) made by different types of objects (list of tuples)

. CREATE a tuple of STRINGS students=(“Kirs”,”Paul”,”Mark”) print(students) . CREATE a tuple of INTEGERS grades=(30,23,25)

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=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” =

ADD a new item to a dictionary

FOR LOOP

Is a mechanism for which computer repeats the same block of codes multiple times

For in :

TRADITIONAL METHOD:

aaa= print(element1=aaa+1) 3

aaa= print(element1=aaa+1) 4

aaa= print(element1=aaa+1) 5

FOR LOOP METHOD:

list1=2,3,4 For aaa in list1: per tutti i valori nella list1: element1=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 list1)

For aaa in range(len(list1)): Element1=list1 aaa + 3 4 5

ITERATE OVER A LIST

Two different ways to ITERATE over a LIST:

  1. for aaa in list1 -> the iterator “aaa” indicates the aaa-th element in list 1
  2. for aaa in range(len(list1)) -> the iterator “aaa” is the index of the aaa-th element in list 1

ITERATE OVER DICTIARES

CHAMPIONS= ”Real”: 13,”Milan”:7,“Juve”:2 

RETRIVE:

  • both KEYS and VALUES (=ITEMS) two iterators, dictionary.items() For k,v in champions.items(): Print(k,v) Real 13 Milan 7 Juve 2
  • just the VALUES of the dictionary one iterator, dictionary.values() For v in champions.values(): Print(v) 13 7 2
  • just the KEYS of a dictionary one iterator, dictionary.keys() For v in champions.keys(): Print(v) Real Milan Juve

ITERATE OVER MULTILEVEL DICTIONARIES

Students= ”Kris”: ”grade”: 30,”skill”: “math” , ”Paul”: ”grade”: 29,”skill”: “latin” , ”Mark”: ”grade”: 22,”skill”: “engineering”  

RETRIVE:

  • ITEMS of multilevel dictionary

FUNCTIONS

Is a block of resuable code that you can use to perform specific action.

Def ():   return

  1. Definition
  2. Calling
  3. 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* return g
  4. 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” =new_value

ADD

name_object “New_element” =value

DELETE

delList_name index 

SUM

element1+element sum(list_name) OR sum(element1,element2)

LENGTH (number of elements) len(object)

BOOLEAN (check if 2 objects are the same) object1==object

RETRIEVE (select an element of a list) list_name index 

  • forward methods 0,1,2,…,…
  • negative methods …,…,-3,-2,-1 

SLICE (select few elements of a list) list_name index_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)

FOR LOOP:

  1. Create list1 made by 4 integers
  2. iterate over list1. take each element of list1, add 1 and assign the result to an object called element
  3. at the end of the iteration, we print the resulting element1 from the last iteration
  4. We create 3 lists with 3 elements each
  5. iterate over 3 lists. for each iteration, concatenate the i-th element of these lists. we cast the i- th element of the list followers, because it is an integer
  6. We create 3 lists
  7. We create/initiate an empty list
  8. we iterate over 3 lists. for each iteration, we create an object "complete" that is the concatenation of the i-th element of the 3 lists, append "complete" to the empty list that we initiate.
  9. at the end of the iteration, we print the final list twitter

FUNCTION:

  1. Define a function guess1 with one parameter called n
  2. Create a function that subtract 4 from n
  3. Print the result of 5-