You are on page 1of 19

PYTHON

VARIABLES
name = "John"
age = "13"
print("His name is " + name + " and he is " + age + " years
old.");

STRINGS
print("Rares e cel mai \n BOSS")
print("Rares a citit \"Harry Potter\"")
phrase = "Rares e BOSS"
print(phrase)
print(phrase + "si jmecher")
print(phrase.lower())
print(phrase.upper())
print(phrase.isupper())
print(phrase.islower())
print(phrase.upper().isupper())
print(len(phrase))
print(phrase[0])
print(phrase.index("R"))
print(phrase.index("BOSS"))
print(phrase.replace("BOSS", "jmecher"))

NUMBERS
from math import*
print(3+5)
print(10%3)
num = 4
print(str(num) + " is my favourite number.")
print(abs(-21))
print(pow(3,2))
print(max(3,6))
print(min(4,8))
print(round(6.2))
print(ceil(8.1))
print(floor(4.9))
print(sqrt(81))

USER INPUT

name = input("Enter name: ")


print("Your name is " + name)

BASIC CALCULATOR
*for whole numbers we use int
*for decimal numbers we use float

To put 2 values next to each other


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

To add 2 numbers
num1 = input("Enter a number: ")
num2 = input("Enter another number: ")
result = float(num1) + float(num2)
print(result)
MAD LIMBS
a = input("Enter a color: ")
b = input("Enter a vegetable: ")
c = input("Enter a name: ")
print("My dick is " + a)
print("My dick look like a " + b)
print("And his name is " + c)

LISTS
friends = ["a", "b", "c", "d", "e"]
print(friends)
print(friends[0])
print(friends[-1])
print(friends[1:])
print(friends[1:3])
friends[1] = "z"
print(friends[1])

LISTS FUNCTIONS
numbers = [0, 4, 8, 98, 32, 78]
friends = ["a", "b", "c", "d", "e"]
friends.extend(numbers)
print(friends)

numbers = [0, 4, 8, 98, 32, 78]


friends = ["a", "b", "c", "d", "e"]
friends.append("z")
print(friends)
numbers = [0, 4, 8, 98, 32, 78]
friends = ["a", "b", "c", "d", "e"]
friends.insert( 2, "z")
print(friends)

numbers = [0, 4, 8, 98, 32, 78]


friends = ["a", "b", "c", "d", "e"]
friends.remove("a")
print(friends)

numbers = [0, 4, 8, 98, 32, 78]


friends = ["a", "b", "c", "d", "e"]
friends.clear()
print(friends)

numbers = [0, 4, 8, 98, 32, 78]


friends = ["a", "b", "c", "d", "e"]
friends.pop()
print(friends)

numbers = [0, 4, 8, 98, 32, 78]


friends = ["a", "b", "c","c", "c", "d", "e"]
print(friends.index("a"))
print(friends.count("c"))

numbers = [0, 4, 8, 98, 32, 78]


friends = ["c", "b", "a","c", "c", "d", "e"]
friends.sort()
print(friends)
numbers.sort()
print(numbers)
numbers = [0, 4, 8, 98, 32, 78]
friends = ["c", "b", "a","c", "c", "d", "e"]
numbers.reverse()
print(numbers)
friends.reverse()
print(friends)

numbers = [0, 4, 8, 98, 32, 78]


friends = ["c", "b", "a","c", "c", "d", "e"]
friends2 = friends.copy()
print(friends2)

TUPLES
coordinates = [(4, 5), (5, 90), (45, 78)]
print(coordinates[0])
coordinates2 = (6, 98)
print(coordinates2[0])

FUNCTIONS
def sayhi(name, age):
print("Hello " + name + " and you are " + str(age) + "
years old")
sayhi("Mike", 45)
sayhi("Steve", 89)

RETURN STATEMENT
def cube (num):
result = num * num * num
return result
# because we used the return statement the print function
doesn t work
print("code")

print(cube(89))

IF STATEMENT
male = input("You are a male(yes/no): ")
tall = input("You are tall(yes/no): ")
male = male.upper()
tall = tall.upper()

if male == "YES" and tall == "YES":


print("You are a tall male")
elif male == "YES" and tall == "NO":
print("You are a short male")
elif male == "NO" and tall == "YES":
print("You are a tall female")
elif male == "NO" and tall == "NO":
print("You are a short female")
else:
print("You are to retard to respond to those questions
correctly")

IF STATEMENT WITH NUMBERS


from time import *
def max(num1, num2, num3):
if num1 > num2 and num1 > num3:
return num1 + " is the biggest"
if num2 > num1 and num2 > num3:
return num2 + " is the biggest"
if num3 > num2 and num3 > num1:
return num3 + " is the biggest"
else:
return "They are all equall"
a = input("Enter a number: ")
b = input("Enter another number: ")
c = input("Enter another number: ")
def countdown(n):
while n > 0:
print(n)
n=n-1
import time
def countdown(n):
while n > 0:
print(n)
time.sleep(1)
n -= 1
countdown(3)
print ("Your max number is ready")
print(max(a, b, c))

COUNTDOWN
def countdown(n):
while n > 0:
print(n)
n=n-1
import time
def countdown(n):
while n > 0:
print(n)
time.sleep(1)
n -= 1
countdown(3)
print ("Your max number is ready")

CALCULATOR
num1 = float(input("Enter a number: "))
op = input("Enter a operator: ")
num2 = float(input("Enter another number: "))
if op == "+":
print(num1 + num2)
elif op == "-":
print(num1 - num2)
elif op == "/":
print(num1 / num2)
elif op == "*":
print(num1 * num2)
else:
print("Invalid operator")

DICTIONAIRES

month = {
"ian":"ianuarie",
"feb":"februarie",
"mar":"martie",
"apr":"aprilie",
"mai":"mai",
"iun":"iunie",
"iul":"iulie",
"sep":"septembrie",
"oct":"octombrie",
"noi":"noiembrie",
"dec":"decembrie",

}
print(month["ian"])
input1 = input("Enter a abreviation: ")
input1 = input1.lower()
print(month.get(input1, "Not a valid word"))

WHILE LOOP
i=1
while i <= 10:
print(i)
i += 1
print("Done")

GUESSING GAME
word = "potato"
guess = ""
guesses = 0
limit = 3
a = False

print("What is the most popular vegetable ?")


while word != guess and not a:
if guesses < limit:
guess = (input("Enter your guess: ").lower())
guesses +=1
if guess == word:
if guesses == 1:
print ("YOU WON FIRST TRY!!")
else:
print("You won with only " + str(guesses) + "
guesses \n GOOD JOB!!")

elif word != guess:


lives = limit - guesses
if lives > 1:
print("Wrong and you have " + str(lives) + "
guesses remaining")
elif lives > 0 :
print("Be careful!! THIS IS YOUR LAST GUESS!!")
else:
a = True
if a:
print("You are out of guesses!! \n The word was
\"potato\"")
else:
print("")

FOR LOOP
for a in "I like ez":
print(a)

friends = ["a", "b", "c", "f"]


for friend in friends:
print(friend)

for number in range(10):


print(number)

for number in range(3, 10)


print(number)

friends = ["a", "b", "c", "f"]


for a in range(len(friends)):
print(friends[a])
friends = ["a", "b", "c", "f"]
for a in range(len(friends)):
if a == 0:
print("First")
else:
print("Not first")

EXPONENT FUNCTION
def mue (num1, num2):
result = 1
for a in range (num2):
result = result * num1
return result
num3 = int(input("Enter the base number: "))
num4 = int(input ("Enter the exponent: "))
print(mue(num3, num4))

2 D LISTS AND NESTED LOOPS


numbers = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
[0]
]
print(numbers[0][0])
for row in numbers:
for col in row:
print(col)
BUILD A TRANSLATOR
def translate(phrase):
translation = ""
for letter in phrase:
if letter.upper() in "AEIOU":
if letter.isupper():
translation = translation + "G"
else:
translation = translation + "g"
else:
translation = translation + letter
return translation
on = input("Enter a phrase: ")
print(translate(on))

TRY/EXCEPT (CATCH AN ERROR)


try:
value = 10/0
num = int(input("Enter a number: "))
print(num)
except ZeroDivisionError:
print("Divided by 0")
except ValueError:
print("Invalid input")

try:

num = int(input("Enter a number: "))


print(num)
except ZeroDivisionError:
print("Divided by 0")
except ValueError:
print("Invalid input")
try:
num = int(input("Enter a number: "))
print(num)
except ValueError as err:
print(err)

READING FILES
“r” – read
“w” – write
“a” – append (add new info)
“r+” – read and write
a = open("sd.txt", "r")
print(a.read())
a.close()

a = open("sd.txt", "r")
print(a.readline())
print(a.readline())
a.close()

a = open("sd.txt", "r")
print(a.readlines())
a.close()

a = open("sd.txt", "r")
print(a.readlines()[0])
a.close()
a = open("sd.txt", "r")
for employee in a.readlines():
print(employee)
a.close()

WRITING TO FILES
Appending
a = open("sd.txt", "a")
a.write("\nJom - gay")
a.close

Write is used to overwrite the file


a = open("sd.txt", "w")
a.write("\nJom - gay")
a.close

Because I wrote “sd1.txt” python created a new file


a = open("sd1.txt", "w")
a.write("\nJom - gay")
a.close

Now I created a new html file


a = open("index.html", "w")
a.write("<p>Hi</p>")
a.close
MODULES AND PIPS
Main page
import tools
print(tools.roll_dice(10))

Tools page
import random
feet_in_mile = 5280
meters_in_kilometers = 1000
beatles = [" John Lennon", "Paul McCartney", "Ringo Starr"]

def get_file_name(filename):
return filename[filename.index(".") + 1:]

def roll_dice(num):
return random.randint(1, num)

CLASSES AND OBJECTS


Main page
from Student import Student
student1 = Student("Jim", "Business", 3.1, False)
print(student1.name)
student2 = Student("Pam", "Art", 2.5, True)

Student page
class Student:
def __init__ (self, name, major, gpa, is_on_probation):
self.name = name
self.major = major
self.gpa = gpa
self.is_on_probation = is_on_probation

BUILD A MULTIPLE CHOICE QUIZ


Main page
from Question import Question
questions_prompts = ["What color are red apples?
\na)red\nb)yellow\nc)green\n",
"What color are
bananas?\na)blue\nb)green\nc)yellow\n",
"What color are
oranges?\na)blue\nb)orange\nc)grey\n"]
questions = [Question(questions_prompts[0], "a"),
Question(questions_prompts[1], "c"),
Question(questions_prompts[2], "b")
]
def run_test(question):
score = 0
for question in questions:
answer = input(question.prompt)
if answer == question.answer:
score +=1
print("You got " + str(score) + "/" +
str(len(questions_prompts)))
run_test(questions)

Question page
class Question:
def __init__(self, prompt, answer):
self.prompt = prompt
self.answer = answer

OBJECT FUNCTIONS
Main page
from Student1 import Student1
student1 = Student1("Oscar", "Accounting", 3.1)
student2 = Student1("Phyllis","Business", 3.8)
print(student1.honor())
print(student2.honor())

Student1 page
class Student1:
def __init__(self,name,major,gpa):
self.name =name
self.major =major
self.gpa =gpa
def honor(self):
if self.gpa >= 3.5:
return True
else:
return False
INHERITANCE
Main page
Very important!!
Put “()” after (ex “mychef = Chef()”)
from Chef import Chef
from ChineseChef import ChineseChef
mychef = Chef()
mychef.make_chicken()
mychef1 = ChineseChef()
mychef1.make_fried_rice()
mychef2 = ChineseChef()
mychef2.make_special_dish()

Chef page
class Chef:
def make_chicken(self):
print("The chef makes chicken")
def make_salad(self):
print("The chef makes salad")
def make_special_dish(self):
print("The chef bbq ribs")

Chinese chef page


from Chef import Chef
class ChineseChef(Chef):
def make_special_dish(self):
print("The chef makes orange chicken")
def make_fried_rice(self):
print("The chef makes fried rice")

You might also like