Last active
January 29, 2018 16:16
-
-
Save ivalexandru/f7bc1e74643998ad0db693fe45368eaa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
parrot_list = ['non pinin', 'no more'] | |
parrot_list.append("norwegian blue") #adauga la sfarsitul listei | |
for state in parrot_list: | |
print('This parrot is ' + state) | |
odd = [1, 3, 5, 7] | |
even = [2, 4, 6, 8] | |
numbers = odd + even | |
numbers.sort() #sort nu creaza un obiect nou | |
print(numbers) | |
#poti pune direct print(sorted(numbers)) #creeaza un obj nou | |
#poti crea o lista asa: | |
#creezi o lista care sa contina literele pe rand | |
print(list("The lists are equal")) | |
literele = list("The lists are equal") | |
print(literele) | |
#DACA PUI = INTRE DOUA VARIABILE, APOI MODIFICI UNA DIN ELE, | |
#VEI MODIFICA INDIRECT SI CEALALTA VARIABILA: | |
even = [2, 4, 6, 8] | |
another_even = even | |
another_even.sort(reverse= True) #sorteaza de la mare la mic | |
print(even) #am printat var INITIALA | |
#dar rezultatul este tot 8,6,4,2 | |
#aceste 2 variabile sunt IDENTICE: | |
#ps, nu confunda is cu == | |
#is confirma ca sunt identice | |
# == confirma ca ele contin aceleasi valori | |
print(even is another_even) #prints True | |
menu = [] | |
menu.append(['salam', 'parizer', 'spam']) | |
menu.append(['banane', 'portocale', 'spam']) | |
menu.append(['biscuiti', 'ciocolata', 'ecler']) | |
#print(menu) | |
#cauta spam in fiecare sublista | |
#si printeaza doar sublista fara spam | |
for meal in menu: | |
if 'spam' not in meal: | |
#print(meal) #printeaza sublista | |
for item in meal: | |
print(item) #printeaza elementele sublistei pe rand nou | |
list = [] | |
list.insert(0, 5) | |
list.insert(1, 10) | |
list.insert(0, 6) #cand inserez 6 aici, nu il inlocuieste pe 5-ul pus mai sus | |
#ci ii shifteaza pe 5 si pe 10 mai la 'dreapta' | |
print(list) # prints [6,5,10] | |
list.remove(6) #sterge PRIMUL 6, nu pe toti | |
list.append(9) | |
list.append(1) | |
list.sort() | |
print(list) #[1,5,9,10] | |
list.pop() #daca nu ii dai numar, removes the last item | |
list.reverse() | |
print(list) | |
"""Write a Python program to remove duplicates from a list of lists | |
Sample list : [[10, 20], [40], [30, 56, 25], [10, 20], [33], [40]]""" | |
lista_initiala = [[10, 20], [40], [30, 56, 25], [10, 20], [33], [40]] | |
rezultat = [] | |
for i in lista_initiala: | |
if i not in rezultat: | |
rezultat.append(i) | |
print(rezultat) | |
#Write a Python program to sum all the items in a list. | |
lista5 = [2, 4, 6] | |
suma = 0 | |
for i in lista5: | |
suma += i | |
print(suma) | |
#Write a Python program to get the largest number from a list. | |
#R: sortezi lista si apoi [-1] | |
lista6 = [9, 7, 5, 2] | |
lista6.sort() | |
print(lista6[-1]) | |
#varianta2 | |
def max_num_in_list( list ): | |
max = list[ 0 ] | |
for a in list: | |
if a > max: | |
max = a | |
return max | |
print(max_num_in_list([9, 7, 5, 2])) | |
#Return the smallest number from a list | |
lista7 = [3, 4, 1] | |
mic = lista7[0] | |
for i in lista7: | |
if i < mic: | |
mic = i | |
print(mic) | |
"""Write a Python program to COUNT the number of strings where the string length is 2 or more | |
and the first and last character are same from a given list of strings. | |
Sample List : ['abc', 'xyz', 'aba', '1221'] | |
Expected Result : 2 """ | |
#LA IF FOLOSESTI MIAU, NU LISTA8 | |
#ASA ACCESEZI ELEMENTELE INDIVIDUALE DIN LISTA | |
lista8 = ["salam", "parizer", "papaya", "ciocolatac", "z"] | |
numaratoare = 0 | |
for miau in lista8: | |
if len(miau) > 1 and miau[0] == miau[-1]: | |
numaratoare += 1 | |
print(numaratoare) | |
"""Write a Python program to get a list, sorted in increasing order by | |
the last element in each tuple from a given list of non-empty tuples. | |
Sample List : [(2, 5), (1, 2), (4, 4), (2, 3), (2, 1)] | |
Expected Result : [(2, 1), (1, 2), (2, 3), (4, 4), (2, 5)] """ | |
lista_initiala = [(2, 5), (1, 2), (4, 4), (2, 3), (2, 1)] | |
print(type(lista_initiala)) #prints LIST | |
lista_initiala.sort() | |
print(lista_initiala) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment