My Python Journey

Useful Operators

We can use the Range function to create a range of numbers in a list instead of typing them manually

mylist = list(range(0,11))

for num in mylist:
    if num % 2 == 0:
        print(num)
    else:
        print('\n')

The Enumerate function will allow us to index items in a list and return them as tuples. We can then use tuple unpacking

my_letters = 'a','b','c'

# This is a basic for loop
for case in my_letters:
    print(case)

#Here we use the enumerate function
for case in enumerate(my_letters):
    print (case)

# Here we use the enumerate function with tuple unpacking
for i, letter in enumerate(my_letters):
    print("At index {}, the letter is {}".format(i,letter))

The Zip function allows us to take multiple lists and zip them up into tuples

# First, lets make a list from these tuples

print(list(enumerate('x, y,z')))

mylist1 =list(range(1,6))
mylist2 = ['a','b','c','d','e']

# Here I am creating a new list that has the contents of the two lists
mylist3=list(zip(mylist1,mylist2))

# Lets use a for loop to iterate

for item1,item2 in zip(mylist1, mylist2):
    print("For the number {} location the letter is {}".format(item1,item2))


# We can use the In operator for other things, such as checking to see if something is in something

print('x' in 'x-ray')
print(2 in [1,2,3])

Random Numbers with Python

# Python has a builtin in random number generator, however we need to import

# This will import a shuffle function to scramble numbers around
from random import shuffle

shuffle(mylist)
print(mylist)

# We can use the randint to create a pyshdo-random number
from random import randint

my_random = randint(0,1000)

print(my_random)

Getting input from users

# If we want to get some input from the user we can used the input

intel = input('Enter something into this box: ')

print(intel)