Write a code that checks if a number, x, is non-negative or negative. If it is a non-negative number your program should print the value of that variable preceding the text 'is non-negative'. On the other hand, if it's a negative number, your program should print the value of that variable preceding the text 'is negative'. For example, if x = 1.7 then the program should print: '1.7 is non-negative', however if x = -1.7 then the program should print: '-1.7 is negative'.

In [6]:
x = 12
In [9]:
if x >= 0:
  print(f"{x} is non-negative")
  y = 1
else:
  print(f"{x} is negative")
  y = -1
print(y)
12 is non-negative
1
In [7]:
print(f"{x} is non-negative" if x >= 0 else f"{x} is negative")
12 is non-negative
In [16]:
x = -1
y = 1 if x >= 0 else -1
print(y)
-1

Write a program which creates a list of the powers of three for the exponents in the range [0, 12]

In [19]:
lst = [3**k for k in range(13) if k % 2 == 0]
print(lst)
[1, 9, 81, 729, 6561, 59049, 531441]

Write a program that creates a list named lst that contains all the non-negative two-digits integers whose digits sum up to 6.

In [20]:
 lst = [n for n in range(10,100) if sum(divmod(n, 10)) == 6]
 print(lst)
[15, 24, 33, 42, 51, 60]

Write a program that accepts a comma separated sequence of words as input and prints the words in a comma-separated sequence after sorting them alphabetically.

Suppose the following input is supplied to the program: without,hello,bag,world Then, the output should be: bag,hello,without,world

In [24]:
items = [x for x in input().split(',')]
z = items.sort()
print(','.join(items))
print(z)
without,hello,bag,world
bag,hello,without,world
None

Differences between the remove method and pop¶

In [ ]:
students = ["itay", "Roy", "Alon", "Zohar", "Danielle"]
print(students)
x = students.remove("Alon")
print(students, x, sep="\n")
['itay', 'Roy', 'Alon', 'Zohar', 'Danielle']
['itay', 'Roy', 'Zohar', 'Danielle']
None
In [26]:
students = ["itay", "Roy", "Alon", "Zohar", "Danielle"]
print(students)
x = students.pop(2)
print(students)
y = students.pop(2)
print(students, x, y, sep="\n")
['itay', 'Roy', 'Alon', 'Zohar', 'Danielle']
['itay', 'Roy', 'Zohar', 'Danielle']
['itay', 'Roy', 'Danielle']
Alon
Zohar

The differences between the function sorted and the method sort¶

In [27]:
lst = [1, 0, -7, 2, 3, -9.1]
print(lst, end="\n\n")
lst_sorted = sorted(lst)
print(lst, lst_sorted, sep="\n")
[1, 0, -7, 2, 3, -9.1]

[1, 0, -7, 2, 3, -9.1]
[-9.1, -7, 0, 1, 2, 3]
In [28]:
lst = [1, 0, -7, 2, 3, -9.1]
print(lst, end="\n\n")
lst_sorted = lst.sort()
print(lst, lst_sorted, sep="\n")
[1, 0, -7, 2, 3, -9.1]

[-9.1, -7, 0, 1, 2, 3]
None

Write a program, which will find all such numbers between 1000 and 3000 (both included) such that each digit of the number is an even number. The numbers obtained should be printed in a comma-separated sequence on a single line.

In [21]:
values = []
for i in range(1000, 3001):           
    s = str(i)
    if ((int(s[0]) % 2 == 0) and (int(s[1]) % 2 == 0) 
    and (int(s[2]) % 2 == 0) and (int(s[3]) % 2 == 0)):
        values.append(s)
        
print(",".join(values))
2000,2002,2004,2006,2008,2020,2022,2024,2026,2028,2040,2042,2044,2046,2048,2060,2062,2064,2066,2068,2080,2082,2084,2086,2088,2200,2202,2204,2206,2208,2220,2222,2224,2226,2228,2240,2242,2244,2246,2248,2260,2262,2264,2266,2268,2280,2282,2284,2286,2288,2400,2402,2404,2406,2408,2420,2422,2424,2426,2428,2440,2442,2444,2446,2448,2460,2462,2464,2466,2468,2480,2482,2484,2486,2488,2600,2602,2604,2606,2608,2620,2622,2624,2626,2628,2640,2642,2644,2646,2648,2660,2662,2664,2666,2668,2680,2682,2684,2686,2688,2800,2802,2804,2806,2808,2820,2822,2824,2826,2828,2840,2842,2844,2846,2848,2860,2862,2864,2866,2868,2880,2882,2884,2886,2888

enumerate and zip functions¶

In [42]:
lst1 = ["a", "b", "c", "d"]
# i = 0
for i, ele in enumerate(lst1): # [(0, "a"), (1, "b"), (2, "c"), (3, "d")]
  print(ele, i)
  # i += 1

# lst1 = ["a", "b", "c", "d"]
# i = 0
# for ele in lst1:
#   print(ele, i)
#   i += 1

print(enumerate(lst1))
print(list(enumerate(lst1)))
a 0
b 1
c 2
d 3
<enumerate object at 0x7f6cbd327100>
[(0, 'a'), (1, 'b'), (2, 'c'), (3, 'd')]
In [34]:
lst1 = ["a", "b", "c", "d"]
lst2 = [10, 20, 30, 40]
# for ele1 in lst1: 
#   for ele2 in lst2:
#     print(ele1, ele2)

for ele1, ele2 in zip(lst1, lst2): # [("a", 10), ("b", 20), ...]
  print(ele1, ele2)

print(zip(lst1, lst2))
print(list(zip(lst1, lst2)))
a 10
b 20
c 30
d 40
<zip object at 0x7f6cbd2fcf80>
[('a', 10), ('b', 20), ('c', 30), ('d', 40)]
In [39]:
vector1 = [1, 2, 3]
vector2 = [0, 1, 0]
scalar_prod = 0
for e1 ,e2 in zip(vector1, vector2):
  scalar_prod += e1*e2

print(vector1, vector2, scalar_prod, sep=", ")
[1, 2, 3], [0, 1, 0], 2