My Python Notes

Python Essentials 1


One more time, these notes are not meant to be useful for anyone else but me. I am not caring that much about formatting or presentation. I am posting it as a record that I am doing something (which motivates me to keep learning) and as future quick reference instead of going to Google. ( Like this at least I visit my blog :( )

So, here it is…


append.py

print("=========================\n")
### Create a list
my_list = [1, 2, 3, 4, 5]

### Print the list
print(my_list)

### print the lenght of the list
print(len(my_list))

print("=========================\n")
### Append 6 to the end of the list
my_list.append(6)

print(my_list)
print("The lenght of my_list is: ", len(my_list))

print("=========================\n")

my_list.append(7)
print(my_list)
print("The lenght of my_list is: ", len(my_list))

print("=========================\n")

my_list.insert(7, 99)
print(my_list)

bitwise.pycat bitwise.py

x = 4
y = 1

a = x & y

# a = 0100 & 0001
# a = 0000
# a = 0

b = x | y

# b = 0100 | 0001
# b = 0101
# b = 5

c = ~x        # tricky!

# c =  ~(0100)
# c =    1011
# c = 11

d = x ^ 5

# d = 0100 ^ 0101
# d = 0001
# d = 1

e = x >> 2

# e  = 0100 >> 2
# e  = 0001
# e = 1


f = x << 2

# f = 0100 << 2
# f = 010000
# f = 16



print(a, b, c, d, e, f)


x = 4
y = 1

a = x & y

# a = 0100 & 0001
# a = 0000
# a = 0

b = x | y

# b = 0100 | 0001
# b = 0101
# b = 5

c = ~x        # tricky!

# c =  ~(0100)
# c =    1011
# c = 11

d = x ^ 5

# d = 0100 ^ 0101
# d = 0001
# d = 1

e = x >> 2

# e  = 0100 >> 2
# e  = 0001
# e = 1


f = x << 2

# f = 0100 << 2
# f = 010000
# f = 16



print(a, b, c, d, e, f)

break.py

print("The break instruction")
for i in range(1,6):
    if i == 3:
        break
    print("Inside the loop.", i)
print("Outside the loop")

continue.py

print("\n The continue instruction:")
for i in range(1,6):
    if i == 3:
        continue
    print("Inside the loop.", i)
print("Outside the loop.")

delete.py

numbers = [10, 5, 7, 2, 1]
print("Original list content:", numbers)  # Printing original list content.

numbers[0] = 111
print("\nPrevious list content:", numbers)  # Printing previous list content.

numbers[1] = numbers[4]  # Copying value of the fifth element to the second.
print("Previous list content:", numbers)  # Printing previous list content.

print("\nList's length:", len(numbers))  # Printing previous list length.



del numbers[1]  # Removing the second element from the list.
print("New list's length:", len(numbers))  # Printing new list length.
print("\nNew list content:", numbers)  # Printing current list content.

empty_list.py

my_list = [] # Create an empty list

for i in range(10):
    my_list.append(i+1)
print(my_list)


##
print("============================\n")



my_list = [] # Creating an empty list.

for i in range(10):
    my_list.insert(0, i + 1)

print(my_list)

break.py

for ch in "john.smith@pythoninstitute.org":
    if ch == "@":
        break
    print(ch, end=" ")

guess_number.py


cat guess_number.py

print(
"""
+================================+
| Welcome to my game, muggle!    |
| Enter an integer number        |
| and guess what number I've     |
| picked for you.                |
| So, what is the secret number? |
+================================+
""")

number_to_guess = 10

number_entered_by_user = int(input("Try to guess the magician number: "))

while number_to_guess != number_entered_by_user:

    print("Ha ha! You're stuck in my loop!\n")
    number_entered_by_user = int(input("Try to guess the magician number: "))

print("Well done, muggle! You are free now.\n")

indexing.py

numbers = [10, 5, 7, 2, 1]
print("Original list contents: ", numbers)


numbers[0] = 111
print("Original list contents: ", numbers)


numbers[1] = numbers[4]
print("Original list contents: ", numbers)


numbers_len = len(numbers)
print(numbers_len)

iterating.py


example = [10, 40, 20, 20, 20]

total = 0

for i in range(len(example)):
    total += example[i]
print(total)

## In the below example, the variable "i" takes the value of the item in the list. Unlike the previous example
## where "i" was taking the index.

print("====================")
print("====================")

my_list = [10, 1, 8, 3, 5]
total = 0

for i in my_list:
    total += i


print(total)

loop1.py

for i in range (2,8):
    print("The value of i is currently:", i)

print("=================================n")


## For with 3 arguments

for j  in range(2,8,3):
    print("The value of j is currently:", j)

mississippis_count.py

import time

# Write a for loop that counts to five.
i = 1
for i in range(6):
        # Body of the loop - print the loop iteration number and the word "Mississippi".
    print(i, "Mississippi")
    # Body of the loop - use: time.sleep(1)
    time.sleep(1)

# Write a print function with the final message.

more_practicing.py

# step 1: create an empty list named beatles;

beatles = []

# step 2: use the append() method to add the following members of
# the band to the list: John Lennon, Paul McCartney, and George Harrison;

beatles.append("John Lennon")
beatles.append("Paul McCartney")
beatles.append("George Harrison")

print(beatles)


# step 3: use the for loop and the append() method to prompt the user
# to add the following members of the band to the list: Stu Sutcliffe, and Pete Best;


number_of_members_to_add = int(input("Please, specify the amount of members to add: "))

i = 0
for i in range(number_of_members_to_add):
    beatles.append(input("Please, enter the name of the Beatle member to add: "))
print(beatles)


# step 4: use the del instruction to remove Stu Sutcliffe and Pete Best from the list;

del beatles[3:]

print(beatles)


# step 5: use the insert() method to add Ringo Starr to the beginning of the list.

beatles.insert(0, "Ringo Starr")
print(beatles)

odd_even.py

# A program that reads a sequence of numbers
# and counts how many numbers are even and how many are odd.
# The program terminates when zero is entered.

odd_numbers = 0
even_numbers = 0

# Read the first number.
number = int(input("Enter a number or type 0 to stop: "))

# 0 terminates execution.
while number != 0:
    # Check if the number is odd.
    if number % 2 == 1:
        # Increase the odd_numbers counter.
        odd_numbers += 1
    else:
        # Increase the even_numbers counter.
        even_numbers += 1
    # Read the next number.
    number = int(input("Enter a number or type 0 to stop: "))

# Print results.
print("Odd numbers count:", odd_numbers)
print("Even numbers count:", even_numbers)

odd_with_while.py

x = 1
while x < 11:
    if x % 2 != 0:
        print(x)
    x += 1

for i in range(1, 11):
    if i % 2 != 0:
        print(i)

scenario.py

hat_list = [1, 2, 3, 4, 5]  # This is an existing list of numbers hidden in the hat.

# Step 1: write a line of code that prompts the user

# to replace the middle number with an integer number entered by the user.
hat_list[2] = int(input("Please, enter a number: " ))

# Step 2: write a line of code that removes the last element from the list.

del hat_list[-1]

# Step 3: write a line of code that prints the length of the existing list.

print(len(hat_list))

print(hat_list)

studying.py

# Store the current largest number here.
largest_number = -999999999

# Input the first value.
number = int(input("Enter a number or type -1 to stop: "))

# If the number is not equal to -1, continue.
while number != -1:

    if number > largest_number:

        largest_number = number

    number = int(input("Enter a number or type -1 to stop: "))

# Print the largest number.
print("The largest number is:", largest_number)

swapping.py

my_list = [1, 2, 3, 4, 5]

length= len(my_list)

for i in range(length // 2):
    my_list[i], my_list[length - i - 1] = my_list[length - i - 1], my_list[i]

print(my_list)

 Share!

 
comments powered by Disqus