Python starters
tip
Python eggs for starters.
First off, let's take a quick look how are these three languages different from each other.
- JavaScript
- Python
- Java
function helloWorld() {
  console.log('Hello, world!');
}
msg = ("Hello, world!")
print(msg)
class HelloWorld {
  public static void main(String args[]) {
    System.out.println("Hello, World");
  }
}
Yes! they may be different however, the same principles can be applied.
Let's beginβ
- Here are two different examples that prints the same output.
- var1
- var2
print("Hello, world!")
msg = ("Hello, world!")
print(msg)
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 π
         *
        ***
       *****
      *******
     *********
    ***********
   *************
  ***************
 *****************
*******************