You are on page 1of 9

#adauga hobby la lista

hobbies = []
for num in range(3):
hobby = raw_input("Tell me one of your favorite hobbies: ")
hobbies.append(hobby)
print hobbies

#printare din ""

thing = "spam!"
for c in thing:
print c
word = "eggs!"
for car in word:
print car

#aranjare frumos
phrase = "A bird in the hand..."
for char in phrase:
if char == "A" or char == 'a':
print 'X',
else:
print char,
print

#numar la **2 din lista


numbers = [7, 9, 12, 54, 99]

print "This list contains: "


for num in numbers:
print num
for num in numbers:
print num ** 2

#printare lista cu key-ile


d = {'a': 'apple', 'b': 'berry', 'c': 'cherry'}
for key in d:
print key, d[key]

# printare lista mai mare


list_a = [3, 9, 17, 15, 19]
list_b = [2, 4, 8, 10, 30, 40, 50, 60, 70, 80, 90]

for a, b in zip(list_a, list_b):


if a > b :
print a
else:
print b

#for si while impreuna


fruits = ['banana', 'apple', 'orange', 'tomato', 'pear', 'grape']
print 'You have...'
for f in fruits:
if f == 'tomato':
print 'A tomato is not a fruit!' # (It actually is.)
break
print 'A', f
else:
print 'A fine selection of fruits!'

#executare conditie else


fruits = ['banana', 'apple', 'orange', 'tomato', 'pear', 'grape']
print 'You have...'
for f in fruits:
if f == 'tomato':
print 'A tomato is not a fruit!' # (It actually is.)
print 'A', f
else:
print 'A fine selection of fruits!'

#my for/else
test = ["bleh", "blah", "bloh"]
for x in test:
print x
else:
print "ok"

# fuctie cu % -operator pt numere care se divid


def is_even(x):
if x % 2 == 0:
return True
else:
return False

#fuctie care interogheaza numaru


def is_int(x):
absolute = abs(x)
rounded = round(absolute)
return absolute - rounded == 0

# fuctie care returneaza example: digit_sum(1234) should return 10 which is 1 + 2 +


3 + 4.
def digit_sum(x):
total = 0
while x > 0:
total += x % 10
x = x // 10
print x
return total

#calculare factorial

def factorial(x):
total = 1
while x>0:
total *= x
x-=1
return total

#numar prim
def is_prime(x):
if x < 2:
return False
else:
for n in range(2, x-1):
if x % n == 0:
return False
return True

#
def reverse(text):
word = ""
l = len(text) - 1
while l >= 0:
word = word + text[l]
l -= 1
return word

#
def anti_vowel(text):
t=""
for c in text:
for i in "ieaouIEAOU":
if c==i:
c=""
else:
c=c
t=t+c
return t

#
score = {"a": 1, "c": 3, "b": 3, "e": 1, "d": 2, "g": 2,
"f": 4, "i": 1, "h": 4, "k": 5, "j": 8, "m": 3,
"l": 1, "o": 1, "n": 1, "q": 10, "p": 3, "s": 1,
"r": 1, "u": 1, "t": 1, "w": 4, "v": 4, "y": 4,
"x": 8, "z": 10}

def scrabble_score(word):
word = word.lower()
total = 0
for letter in word:
for leter in score:
if letter == leter:
total = total + score[leter]
return total

#fuctie care returneaza text cu word


def censor(text, word):
words = text.split()
result = ''
stars = '*' * len(word)
count = 0
for i in words:
if i == word:
words[count] = stars
count += 1
result =' '.join(words)

return result

#
def count(sequence, item):
count = 0
for i in sequence:
if i == item:
count += 1
return count

#
def purify(lst):
res = []
for ele in lst:
if ele % 2 == 0:
res.append(ele)
return res

#
def product(list):
total = 1
for num in list:
total = total * num
return total

#
def remove_duplicates(inputlist):
if inputlist == []:
return []

# Sort the input list from low to high


inputlist = sorted(inputlist)
# Initialize the output list, and give it the first value of the now-sorted input
list
outputlist = [inputlist[0]]

# Go through the values of the sorted list and append to the output list
# ...any values that are greater than the last value of the output list
for i in inputlist:
if i > outputlist[-1]:
outputlist.append(i)

return outputlist

#
def median(lst):
sorted_list = sorted(lst)
if len(sorted_list) % 2 != 0:
#odd number of elements
index = len(sorted_list)//2
return sorted_list[index]
elif len(sorted_list) % 2 == 0:
#even no. of elements
index_1 = len(sorted_list)/2 - 1
index_2 = len(sorted_list)/2
mean = (sorted_list[index_1] + sorted_list[index_2])/2.0
return mean

print median([2, 4, 5, 9])

#print grade
grades = [100, 100, 90, 40, 80, 100, 85, 70, 90, 65, 90, 85, 50.5]
print "Grades:", grades

#print grade dubla 2


grades = [100, 100, 90, 40, 80, 100, 85, 70, 90, 65, 90, 85, 50.5]

def print_grades(grades_input):
for grade in grades_input:
print grade

print_grades(grades)

#print suma de grade din lista


grades = [100, 100, 90, 40, 80, 100, 85, 70, 90, 65, 90, 85, 50.5]

def grades_sum(scores):
total = 0
for score in scores:
total += score
return total

print grades_sum(grades)

#print suma de grade plus media


grades = [100, 100, 90, 40, 80, 100, 85, 70, 90, 65, 90, 85, 50.5]

def grades_sum(scores):
total = 0
for score in scores:
total += score
return total

print grades_sum(grades)

def grades_average(grades_input):
sum_of_grades = grades_sum(grades_input)
average = sum_of_grades / float(len(grades_input))
return average

print grades_average(grades)
#for each score in scores: Compute its squared difference: (average - score) ** 2
and add that to variance.
grades = [100, 100, 90, 40, 80, 100, 85, 70, 90, 65, 90, 85, 50.5]

def print_grades(grades_input):
for grade in grades_input:
print grade

def grades_sum(scores):
total = 0
for score in scores:
total += score
return total

def grades_average(grades_input):
sum_of_grades = grades_sum(grades_input)
average = sum_of_grades / float(len(grades_input))
return average

def grades_variance(grades):
variance = 0
for number in grades:
variance += (grades_average(grades) - number) ** 2
return variance / len(grades)

print grades_variance(grades)

#Define a function, grades_std_deviation, that takes one argument called variance.


return the result of variance ** 0.5
After the function, create a new variable called variance and store the result of
calling grades_variance(grades).
Finally print the result of calling grades_std_deviation(variance).

grades = [100, 100, 90, 40, 80, 100, 85, 70, 90, 65, 90, 85, 50.5]

def print_grades(grades_input):
for grade in grades_input:
print grade

def grades_sum(scores):
total = 0
for score in scores:
total += score
return total

def grades_average(grades_input):
sum_of_grades = grades_sum(grades_input)
average = sum_of_grades / float(len(grades_input))
return average

def grades_variance(grades):
variance = 0
for number in grades:
variance += (grades_average(grades) - number) ** 2
return variance / len(grades)
def grades_std_deviation(variance):
return variance ** 0.5

variance = grades_variance(grades)

print grades_std_deviation(variance)

#Print out the following:


all of the grades
sum of grades
average grade
variance
standard deviation

grades = [100, 100, 90, 40, 80, 100, 85, 70, 90, 65, 90, 85, 50.5]

def print_grades(grades_input):
for grade in grades_input:
print grade

def grades_sum(scores):
total = 0
for score in scores:
total += score
return total

def grades_average(grades_input):
sum_of_grades = grades_sum(grades_input)
average = sum_of_grades / float(len(grades_input))
return average

def grades_variance(grades):
variance = 0
for number in grades:
variance += (grades_average(grades) - number) ** 2
return variance / len(grades)

def grades_std_deviation(variance):
return variance ** 0.5

variance = grades_variance(grades)

for grade in grades:


print grade
print grades_sum(grades)
print grades_average(grades)
print grades_variance(grades)
print grades_std_deviation(variance)

#dictionar
my_dict = {
'name': 'Nick',
'age': 31,
'occupation': 'Dentist',
}
print my_dict.items()

#dictonoar cu key
my_dict = {
'name': 'Nick',
'age': 31,
'occupation': 'Dentist',
}

print my_dict.keys()
print my_dict.values()

#
my_dict = {
'name': 'Nick',
'age': 31,
'occupation': 'Dentist',
}

for key in my_dict:


print key, my_dict[key]

#
doubles_by_3 = [x * 2 for x in range(1, 6) if (x * 2) % 3 == 0]
even_squares = [x ** 2 for x in range(1, 12) if x % 2 == 0]
print even_squares

#lista
cubes_by_four = [x ** 3 for x in range(1, 11) if ((x ** 3) % 4) == 0]
print cubes_by_four

#
to_one_hundred = range(101)
backwards_by_tens = to_one_hundred[::-10]
print backwards_by_tens

#
to_21 = range(1, 22)
odds = to_21[::2]
middle_third = to_21[7:14]

#
print 5 >> 4 # Right Shift
print 5 << 1 # Left Shift
print 8 & 5 # Bitwise AND
print 9 | 4 # Bitwise OR
print 12 ^ 42 # Bitwise XOR
print ~88 # Bitwise NOT

#
print 0b1, #1
print 0b10, #2
print 0b11, #3
print 0b100, #4
print 0b101, #5
print 0b110, #6
print 0b111 #7
print "******"
print 0b1 + 0b11
print 0b11 * 0b11

#binari
one = 0b1
two = 0b10
three = 0b11
four = 0b100
five = 0b101
six = 0b110
seven = 0b111
eight = 0b1000
nine = 0b1001
ten = 0b1010
eleven = 0b1011
twelve = 0b1100

#
print int("1",2)
print int("10",2)
print int("111",2)
print int("0b100",2)
print int(bin(5),2)
# Print out the decimal equivalent of the binary 11001001.
print int("11001001",2)

#printare numar +1 negativ


print ~1
print ~2
print ~3
print ~42
print ~123

#def check_bit4(input):
mask = 0b1000
desired = input & mask
if desired > 0:
return "on"
else:
return "off"

You might also like