Day 1 of #100DaysOfCode

Today I learnt about Python basics, explored the response-request cycle, HTTP

Let's go through the basics of Python

Variables: They are containers in which they store some type of value like string, integer, boolean, etc.

#In python we declare variable like this 
phone = "charger" 
print(phone)
phone = "case"
print(phone)
phone = "headset"
print(phone)

The variable name has been kept the same in the code above, but the output will vary depending on the situation. It happens because in the first instance, we assigned the value of the case to the phone variable, which has a value of charger. As a result, the value will change in the second case, just as it will in the third.

These are some variable declaration styles.

Then, I learnt about string formatting.

phone = "charger"
print("phone has",charger)
print(f"phone has {phone}") #this is string formatting

Type hinting is a formal solution to statically indicate the type of a value within Python code.

box = "football"
print(box)

box = 10
print(box)

In the above code, we have changed the data type of value being stored in "box" and if we want that "box" variable should only store string data type in the entire code. Here we can use type hinting :

box: str="Football"
print(f"we have a {box}")

How to print multiple lines for display using string formatting?

user: str = "Shreyash"
age: int = 19 
location: str = "Delhi"

print(f'''
The user name is {user}
Age of user is {age}
Location of user is {location}''')

Flow Control

age = 100

if age < 16:
    print("You are NOT eligible for a license.")
elif age >= 100:
    print("You are too old to get a license.")
else:
    print("You can apply for a license!")

Using And/Or

planet = "Zortan"

if age < 16 and planet == "Earth":
    # Evaluation - True and False => False
    print("You are NOT eligible for a license on Earth 🌏")
elif age > 16 and planet == "Earth":
    # Evaluation - False and False => False
    print("You can apply for a license on Earth 🌏")
elif age < 16 or planet == "Zortan":
    # Evaluation - True and True => True
    print("You can apply for a Zortanian 🪐 license!!")

To sum up, all that I have learned in Python, I created a game :

"""
Zortan is under attack!!! Thanos has arrived!
---------------------------------------------

Since Zortan is under attack, Louis calls his Avenger friends from earth.
Avengers receive his call and sends 4 avengers to save Zortan.

1. Ironman
4. Black Widow
2. Spiderman
3. Hulk

Each avenger has its attacking power and they have to fight Thanos
to save Zortan.

Avengers can attack only in pairs and get only 3 chances to kill Thanos,
or else Thanos will kill avengers and we loose the game.
"""

# import the stuff we require
from typing import Final

# declare our constants
IRONMAN_ATTACK_POWER: Final[int] = 250
BLACKWIDOW_ATTACK_POWER: Final[int] = 180
SPIDERMAN_ATTACK_POWER: Final[int] = 190
HULK_ATTACK_POWER: Final[int] = 300

# declare other variables
thanos_life = 1500
choice = 0
attack_num = 0

# declare helper messages
WIN_MSG: Final[str] = "You successfully saved Zortan!!! ✨ ✨ ✨"
LOST_MSG: Final[str] = "Thanos killed Avengers and captured Zortan!! 💀 💀 💀"
MSG = """
---------------------------------------------------------------------
Zortan is under attack, choose the pairs no. that will attack Thanos

1) Ironman & Black Widow
2) Black Widow & Spiderman
3) Spiderman & Hulk
4) Hulk & Ironman
---------------------------------------------------------------------
"""

# Start game
while True:

    # First check, should we play the game?
    if thanos_life <= 0 and attack_num <= 3:
        print(WIN_MSG)
        break
    elif attack_num >= 3:
        print(LOST_MSG)
        break

    # Only if we can play, then ask for user input
    print(MSG)
    choice = int(input("Enter your pair no >>> "))

    if choice == 1:
        print("Ironman & Black Widow are attacking Thanos....")
        thanos_life = thanos_life - IRONMAN_ATTACK_POWER - BLACKWIDOW_ATTACK_POWER
        attack_num = attack_num + 1
    elif choice == 2:
        print("Black Widow & Spiderman are attacking Thanos....")
        thanos_life = thanos_life - BLACKWIDOW_ATTACK_POWER - SPIDERMAN_ATTACK_POWER
        attack_num += 1
    elif choice == 3:
        print("Spiderman & Hulk are attacking Thanos....")
        thanos_life = thanos_life - SPIDERMAN_ATTACK_POWER - HULK_ATTACK_POWER
        attack_num += 1
    elif choice == 4:
        print("Hulk & Ironman are attacking Thanos....")
        thanos_life = thanos_life - HULK_ATTACK_POWER - IRONMAN_ATTACK_POWER
        attack_num += 1

For Web Dev(Django)

Getting Data from the Server -

•Each time the user clicks on an anchor tag with an href = value to switch to a new page, the browser makes a connection to the web server and issues a “GET” request - to GET the content of the page at the specified URL.

•The server returns the HTML document to the browser, which formats and displays the document to the user.

TCP Port Numbers -

•A port is an application-specific or process-specific software communications endpoint

•It allows multiple networked applications to coexist on the same server

•There is a list of well-known TCP port numbers

  1. Login - 23

  2. SSH - 22

  3. Telnet - 23

  4. HTTP - 80

  5. HTTPS - 443

HTTP (Hyper Text Transfer Protocol )

•The dominant Application Layer Protocol on the Internet

•Invented for the Web - to retrieve HTML, Images, Documents, etc.

•Extended to handle data in addition to documents - RSS, Web Services, etc.

•Basic Concept: Make a connection - Request a document - Retrieve the document - Close the connection

Created a simple browser in Python

import socket

mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
mysock.connect(('data.pr4e.org', 80))
cmd = 'GET http://data.pr4e.org/page1.htm HTTP/1.0\r\n\r\n'.encode()
mysock.send(cmd)

while True:
    data = mysock.recv(512)
    if len(data) < 1:
        break
    print(data.decode(),end='')

mysock.close()

Resources -

Python - https://youtu.be/jH85McHenvw

Django - https://www.dj4e.com/lessons