Python
To Bottom
# Python
# Declarations / Importations
import random
import sys
import os
myString = ""
myNumber = 1.8
grocery_list = ["Juice", "Tomatoes", "Flour", "Rice", "Catsup"]
other_events = ["Clean House", "Fix Bike", "Goto Movie"]
to_do = [grocery_list, other_events]
to_do2 = other_events + grocery_list
pi_tuple = (3,1,4,1,5,9)
To Top
# Input
print("Enter nom")
name = sys.stdin.readline()
print("Challo", name)
To Top
# Output
print ("Hallo Python")
print("\n" * 5)
my_quote = "\"quoteth\""
my_string = "I say "
print(my_string + my_quote)
print("I don't want a ", end="")
print("newline.")
print(5*1)
print(5/2)
print(5**2)
print(5//2)
print("%c is my %s letter and my number %d number is %.5f" % ('M', "favorish", 1, 1.8))
long_string = "i'll meet you in Poland - baby!"
print(long_string[17:25])
print(long_string[:-5])
print(long_string[-5:])
print(long_string[:4] + " be there.")
print(long_string.capitalize())
print(long_string.find("Poland"))
print(long_string.isalpha())
print(long_string.isalnum())
print(len(long_string))
print(long_string.replace("baby", "doodie"))
print(long_string.strip()) # remove whitespace
quote_string = long_string.split(" ")
To Top
# Arrays
print (grocery_list [1:4])
print ("First Item: ", grocery_list[0])
grocery_list[0] = "Orange Juice"
print ("New first item: ", grocery_list[0])
print(to_do)
print(to_do[1][1])
grocery_list.append("Onions")
grocery_list.insert(1, "Pickles")
grocery_list.remove("Orange Juice")
grocery_list.sort()
print(grocery_list)
grocery_list.reverse()
print(grocery_list)
del grocery_list[4]
print(grocery_list)
print (to_do2)
print (len(to_do2))
print (max(to_do2))
print (min(to_do2))
To Top
# Maps / Dictionaries
super_villians = {"Big Foot": "Veto von Vaughn", "Lex Luth": "Mack Moroue",
"Darth Dot": "Jack Cracker", "Mr. Monster": "Dick", "Anthrax Jack": "Colen",
"Count Chocula": "King Vampire", "Georgey Porgy": "Bush"}
print(super_villians)
print(super_villians["Darth Dot"])
del (super_villians["Lex Luth"])
super_villians["Big Foot"] = "Chewey"
print(len(super_villians))
print(super_villians.get("Big Foot"))
print(super_villians.keys())
print(super_villians.values())
To Top
# Tuples
pi_tuple = (3,1,4,1,5,9)
my_tuple = list(pi_tuple)
new_list = tuple(my_tuple)
print(len(my_tuple),min(my_tuple),max(my_tuple))
To Top
# Conditionals - if, else, elif, and, or, not
age = 20
if age >= 15 :
print("You can drive")
if age >= 21 :
print("You can drive a tractor-trailer")
elif age == 15 :
print("You may use a lerner's permit")
else : print("You can drive a car")
else :
print("You cannot drive")
if ((age >= 9) and (age <= 18)):
print("You must sign up for the Hunger Games")
elif ((age == 44) or (age >= 65)):
print("Go to the hog farm")
#elif not(age == 21):
elif age !=21 :
print("You are not 21")
To Top
# Loops
for x in range(0,10):
print(x, ' ', end="")
print("")
for y in grocery_list:
print(y)
for x in [2,4,8,16,32,64]:
print(x, end="")
num_list = [[1,2,3],[10,20,30,],[100,200,300]]
for x in range(0,3):
for y in range(0,3):
print(num_list[x][y], end="")
i = 1;
while(i <= 20):
if(i%2 == 0):
print(i)
elif(i == 9):
break
else:
i += 1
continue
i += 1
To Top
# Random numbers
random_num = random.randrange(0,100)
while(random_num != 15):
print(random_num, end="")
random_num = random.randrange(0,100)
To Top
# Functions
def add_numbers(numA, numB):
sumNum = numA + numB
return sumNum
print(add_numbers(2, 4))
To Top
# File I/O
test_file = open("test.txt", "wb")
#test_file = open("test.txt", "ab+")
print(test_file.name)
print(test_file.mode)
test_file.write(bytes("Write to test.txt", 'UTF-8'))
test_file.close()
test_file = open("test.txt", "r+")
text_in_file = test_file.read()
print(text_in_file)
test_file.close()
os.remove("test.txt") # delete test.txt
To Top
# Classes
class Animal:
__name = ""
__height = 0
__weight = 0
__sound = ""
def __init__(self, name, height, weight, sound):
self.name = name
self.height = height
self.weight = weight
self.sound = sound
def set_name(self, name):
self.__name = name
def get_name(self):
return self.__name
def set_height(self, height):
self.__height = height
def get_height(self):
return self.__height
def set_weight(self, weight):
self.__weight = weight
def get_weight(self):
return self.__weight
def set_sound(self, sound):
self.__sound = sound
def get_sound(self):
return self.__sound
def get_type(self):
print("Animal")
def toString(self):
return "{} is {} cm tall and {} kilos and says {}".format(self.__name, self.__height, self.__weight, self.__sound)
cat = Animal("Diamond", 33, 10, "meow")
print(cat.toString())
To Top