Sitemap

Python — Basic Review before diving deeper 04

Want to recall the memory of Python easily?

Press enter or click to view image in full size

This is the note taken while I learned Python “again”, hope it helps you too.
There are additional links in the article for you to understand further information.

Quick tip |
Find ⚡for each topic

☘️Directory

  • Exercise 07 — Encryption Program
  • Scope
  • Tests in W3Schools

❤️Good Resources

Bro code | Python full course
ZeroToMastery | Pyhton
Codes in Github

Encryption Program

Exercise 07 — from Bro Code Tutorials

import random
import string

chars = " " + string.punctuation + string.digits + string.ascii_letters
print(chars)
print("-------------------")

chars = list(chars)
print(chars)
print("-------------------")
Press enter or click to view image in full size

Copy the char table and shuffle it to r random one.

key = chars.copy()
print(key)
print("-------------------")

random.shuffle(key)
print(key)
Press enter or click to view image in full size
# Encryption
plain_text = input("Enter something to encrypt: ")
cipher_text = ""

for i in plain_text:
index = chars.index(i)
cipher_text += key[index]

print(f"Origin: {plain_text}")
print(f"Encrypted: {cipher_text}")

# Decrypt
encrypted_text = input("Enter the encrypted message: ")
decrypted_text = ""

for i in encrypted_text:
index = key.index(i)
decrypted_text += chars[index]

print(f"Encrypted: {encrypted_text}")
print(f"Origin: {decrypted_text}")

But there is a problem, the encryption table is different every time.

What will you do to solve the problem?

The Whole Code

import random
import string

chars = " " + string.punctuation + string.digits + string.ascii_letters
chars = list(chars)

key = chars.copy()
random.shuffle(key)

# Encryption
plain_text = input("Enter something to encrypt: ")
cipher_text = ""

for i in plain_text:
index = chars.index(i)
cipher_text += key[index]

print(f"Origin: {plain_text}")
print(f"Encrypted: {cipher_text}")

# Decrypt
encrypted_text = input("Enter the encrypted message: ")
decrypted_text = ""

for i in encrypted_text:
index = key.index(i)
decrypted_text += chars[index]

print(f"Encrypted: {encrypted_text}")
print(f"Origin: {decrypted_text}")

Scope

🔹What variables do I have access to? — ZTM
🔹A variable is only available from inside the region it is created. This is called scope. — W3Schools

Get Yen Wang’s stories in your inbox

Join Medium for free to get updates from this writer.

Scope can generally be divided as four types:

  • Local Scope
  • Parent Local
  • Global Scope
  • Built-in Function

🔸Local Scope

# ✅✅✅
def a():
x = 10
print(x)

a()

Variable x cannot be called outside of the function a.

# ❌❌❌❌❌
def a():
x = 10

print(x)

🔸Parent Local

A.K.A Function in Function
Within the scope the parent function, variable is vailable.

def parent():

x = 10

def child():
print(x)

child()


parent() # 10

🔸Global Scope

Can be used in any scopes.

x = 10

def a():
print(x)

print(x) # 10
a() # 10

You can change x inside of the function too.

x = 300

def a():
x = 200
print(x)

a()

print(x)

🔸Built-in function

x = 5


def parent():

def child():
return sum

return child()


print(parent())
print(x)

🔸Global keyword

If you want to change a global variable in local scope, you can use global keyword.

x = 100


def a():
global x
x = 500


a()

print(x) # 300

🔸nonlocal

Used to reference a variable in the nearest scope. — GeeksforGeeks

def a():
x = "John"
def b():
nonlocal x
x = "hello"
b()
return x

print(a())

If we didn’t use nonlocal…

def a():
x = "a"

def b():
x = "b"
print('inner', x)

b()
print("outer", x)


a()

⚡Testsfor your Python ability!

🔸W3Schools — 25 Quizs

🔸W3Schools — Exercises