A collection of basic to advanced Python concepts, functions and algorithms, that I created for my personal use and reference. Feel free to copy, use and distribute this material as you wish to. All the commands have been checked to be working on Python v3.6 and above. Please use Google, Stack Overflow and GitHub to find solutions to your issues before asking me or anyone else.

— Parth

WORK IN PROGRESS

Hello World

print("Hello World")

The Print Function

print("   /|")
print("  / |")
print(" /  |")
print("/___|")

Variables

print("My name is Rishi")
print("I am 60 years old")
print("I like my name Rishi a lot")
print("But I don't like being 60")

# Updating variables would be a hectic task

character_name = "Rishi"
character_age = "60"
print("My name is " + character_name + ",")
print("I am 60 "+ character_age + " years old")
print("I like my name " + character_name + " a lot")
print("But I don't like being " + character_name)

character_name = "Rishi" # String variable
character_age = 59.6 # Integer/Float variable
is_female = False # Boolean variable

Strings

print("IIT Madras")
print("IIT\\nMadras") # Newline operator
print("IIT\\"Madras") # Using the backslash
print("IIT\\Madras")

phrase = "IIT Madras" 
print(phrase) # Printing a string

print(phrase + " is amazing") # Using comcatenation operator

print(phrase.lower()) # Converts all to lowercase
print(phrase.upper()) # Converts all to uppercase
print(phrase.isupper()) # Checks if everything is uppercase
print(phrase.upper().isupper()) # Converts, then checks

print(len(phrase)) # Prints length of the string

print(phrase[0]) # Prints character at given index
print(phrase[3])

print(phrase.index("M")) # Prints inde of given character
print(phrase.index("Mad"))
print(phrase.index("z")) # Error

print(phrase.replace("Madras", "Bombay") # Replaces part of a string

Numbers

print(2)
print(2.89)
print(-6.88)

print(3 + 4.5) # Addition operator

print(3 * 4 + 5) # Preserves order of operation
print(3 * (4 + 5))

print(10 % 3) # Remainder operator

num = 5
print(num)
print(str(num) + "My fav number") # Printing with strings

print(abs(num)) # Prints absolute value
print(pow(3, 2)) # Raises first number to the power of the second number 
print(max(3, 2)) # Prints out maximum of the two numbers
print(min(3, 2))
print(round(3.2)) # Rounds off the number
print(round(3.7))

from math import * # To import special functions

print(floor(7.8)) # Prints floor integer
print(ceil(7.8)) # Prints ceiling integer
print(sqrt(36)) # Prints square root of the nymber

Inputs

name = input("Enter your name: ")
age = input("Enter your age: ")
print("Hello " + name + "! You are" + age)

Activity - Building a Basic Calculator

num1 = input("Enter a number: ")
num2 = input("Enter another number: ")

result = num1 + num2 # Error
print(result)

result = int(num1) + int(num2)
result = float(num1) + float(num2)

Activity - Creating a Mad Libs Game

print("Roses are red")
print("Violets are blue")
print("I love you")

colour = input("Enter a colour: ")
plural_noun = input("Enter a plural noun: ")
sportsman = input("Enter a sportsman: ")

print("Roses are {colour}") # Pseudocode
print("{plural noun} are blue")
print("I love {sportsman}")

print("Roses are " + colour)
print(plural noun + " are blue")
print("I love " + sportsman)