Skip to main content

Python starters

tip

Python eggs for starters.

First off, let's take a quick look how are these three languages different from each other.

function helloWorld() {
console.log('Hello, world!');
}

Yes! they may be different however, the same principles can be applied.

Let's begin​

  1. Here are two different examples that prints the same output.
print("Hello, world!")
Show output πŸ‘‰
Hello, world!

For statements​

words = ["cat", "dog", "monkey"]

for w in words:
print(w, len(w))
Show output πŸ‘‰
cat 3
dog 3
monkey 6
a = ['Mary', 'had', 'a', 'little', 'lamb']

for i in range(len(a)):

print(i, a[i])
Show output πŸ‘‰
1 had
2 a
3 little
4 lamb
for n in range(2, 10):
for x in range(2, n):
if n % x == 0:
print(n, 'equals', x, '*', n//x)
break
else:
# loop fell through without finding a factor
print(n, 'is a prime number')
Show output πŸ‘‰
3 is a prime number
4 equals 2 * 2
5 is a prime number
6 equals 2 * 3
7 is a prime number
8 equals 2 * 4
9 equals 3 * 3
for num in range(2, 10):

if num % 2 == 0:
print("Found an even number", num)
continue

print("Found an odd number", num)
Show output πŸ‘‰
Found an even number 2
Found an odd number 3
Found an even number 4
Found an odd number 5
Found an even number 6
Found an odd number 7
Found an even number 8
Found an odd number 9
a = []

for x in range(1,11):
b = x**2
a.append(b)

print(a)

Show output πŸ‘‰
[1]
[1, 4]
[1, 4, 9]
[1, 4, 9, 16]
[1, 4, 9, 16, 25]
[1, 4, 9, 16, 25, 36]
[1, 4, 9, 16, 25, 36, 49]
[1, 4, 9, 16, 25, 36, 49, 64]
[1, 4, 9, 16, 25, 36, 49, 64, 81]
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

Array​

def print_triangle(height):
for i in range(1, height + 1):
print(' ' * (height - i) + '*' * (2 * i - 1))

# Example usage
print_triangle(5 * 2)
Show output πŸ‘‰
         *
***
*****
*******
*********
***********
*************
***************
*****************
*******************