Professional Documents
Culture Documents
hobbies = []
for num in range(3):
hobby = raw_input("Tell me one of your favorite hobbies: ")
hobbies.append(hobby)
print hobbies
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
#my for/else
test = ["bleh", "blah", "bloh"]
for x in test:
print x
else:
print "ok"
#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
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 []
# 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 grade
grades = [100, 100, 90, 40, 80, 100, 85, 70, 90, 65, 90, 85, 50.5]
print "Grades:", grades
def print_grades(grades_input):
for grade in grades_input:
print grade
print_grades(grades)
def grades_sum(scores):
total = 0
for score in scores:
total += score
return total
print grades_sum(grades)
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)
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)
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)
#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',
}
#
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)
#def check_bit4(input):
mask = 0b1000
desired = input & mask
if desired > 0:
return "on"
else:
return "off"