4 ** 3 # output is int
2 + 3.4 ** 2 # output is float
(1 + 1j) ** 2 # output is complex
# Define string
a = "Hello, egg world!"
# Find position of 'egg' word
pos = a.index('egg')
# Make new string
new_string = a[:pos] + a[pos+4:]
# Print out to check
print(new_string)
s = "CAGTACCAAGTGAAAGAT"
s.count("A")
a = [1, 2, 3]
b = [4, 5, 6]
a + b
x = 3.7
(x > 3.4 and x <= 6.6) or x == 2
words = {}
words['Table'] = 'Tisch'
words['Chair'] = 'Stuhl'
words['Snake'] = 'Schlange'
words['Chair']
for i in range(2, 51): # just to 50 for this example
# Set is_prime 'flag' to initial value
is_prime = True
# Try and divide i by all values between 2 and i-1, and if none of them
# work, then the value is prime.
for j in range(2, i):
# Check if i is divisible by j
if i % j == 0:
is_prime = False
break
if is_prime:
print(i)
There are other ways of doing this, e.g.:
for i in range(2, 51):
for j in range(2, i):
if i % j == 0:
break
else: # this gets executed if break is not called
print(i)
a = 0
b = 1
while a < 100000:
print(a)
c = a # save a to old variable
a = b
b = c + b
Even simpler:
a, b = 0, 1
while a < 100000:
print(a)
a, b = b, a + b
With a list:
fib = [0, 1]
while fib[-1] + fib[-2] < 100000:
fib.append(fib[-1] + fib[-2])
print(fib)