Professional Documents
Culture Documents
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
BASIC CALCULATOR
*for whole numbers we use int
*for decimal numbers we use float
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)
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()
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
FOR LOOP
for a in "I like ez":
print(a)
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))
try:
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
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)
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
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")