Created
October 11, 2020 00:25
-
-
Save Jxck-S/0488638acdcacf42fb248a55172bb011 to your computer and use it in GitHub Desktop.
Basic Python Examples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Basic Print string | |
print ('something') | |
#Setting Variable | |
age = 25 | |
#Print variable and string | |
print (age, 'add') | |
#basic input | |
age = input('Enter your age: ') | |
#convert variable to int or float | |
age_int = int(age) | |
print (age_int) | |
#input and convert into int or float in one | |
age2 = int(input('Enter your age: ')) | |
print (age2) | |
income = float(input('Enter your income: ')) | |
print (income) | |
#Adding | |
matric_age = 18 | |
grad_age = matric_age + 4 | |
print (grad_age) | |
#Swap variables | |
x, y = y, x | |
#Combine multiple print lines into one output | |
print('One', end='') | |
print('Two') | |
#Seperator | |
print('One', 'Two', 'Three', sep=' * ') | |
#Formating Numbers | |
bigonum = 6542546245.4535324523654 | |
# Regular add Formatting | |
print("Big Number formatted is" + format (bigonum, ' ,f')) | |
# Round Decimals, 2 places | |
print("Big Number rounded is " + format (bigonum, ' .2f')) | |
# Round and formattin | |
print("Big Number rounded and formatted is " + format (bigonum, ' ,.2f')) | |
# Minimum spacing adding | |
print("Big Number rounded and formatted and min " + format (bigonum, ' 30,.2f')) | |
#Make Constants variables and the variables all uppercase | |
#Turtle Graphics | |
import turtle | |
# Directions | |
# Set Window size | |
turtle.setup(640, 480) | |
turtle.bgcolor('grey') | |
turtle.forward(200) | |
turtle.left(90) | |
turtle.forward(200) | |
turtle.right(90) | |
turtle.forward(200) | |
turtle.setheading(175) | |
turtle.hideturtle() | |
turtle.forward(200) | |
turtle.heading() | |
turtle.pencolor('red') | |
turtle.penup() | |
turtle.forward(40) | |
turtle.pendown() | |
turtle.dot() | |
turtle.forward(30) | |
turtle.showturtle() | |
turtle.speed(10) | |
turtle.begin_fill() | |
turtle.fillcolor('green') | |
turtle.circle(100) | |
turtle.end_fill() | |
turtle.write('Hello World') | |
turtle.pensize(5) | |
turtle.forward(30) | |
turtle.goto(0, 0) | |
turtle.pos() | |
#Basic IF | |
sales = float(input('Enter your sales: ')) | |
if sales > 50000: | |
bonus = 500 | |
print ("you get a bonus of 500") | |
#Else IF | |
sci_element = input().lower() | |
if sci_element == "hydrogen": | |
print (1.008) | |
elif sci_element == "helium": | |
print (4.0026) | |
elif sci_element == "lithium": | |
print (6.94) | |
else: | |
print ("Sorry, I don't recognize that element!") | |
#more complicated | |
if age < 18: | |
minors += 1 | |
elif age >= 18 and age <= 64: | |
adults += 1 | |
elif age >= 65: | |
seniors += 1 | |
#Split by seperator | |
# Seperator(' ') pick which part of sentence [1] | |
secondWord = sentence.split(' ')[1] | |
#Setting boolean to true if statements | |
modelYear = 2005 | |
recalled = ((modelYear >= 1995) and (modelYear <= 1998) or (modelYear >= 2004) and (modelYear <= 2006)) | |
norecall = not((modelYear >= 1995) and (modelYear <= 1998) or (modelYear >= 2004) and (modelYear <= 2006)) | |
if recalled: | |
print ('Recalled') | |
elif norecall: | |
print ('Not Recalled') | |
#While Loops | |
k = 0 | |
total = 0 | |
while k < 50: | |
k+= 1 | |
total += k*k | |
#For loop | |
for number in [1, 3, 7, 10]: | |
print (number) | |
for k in range(1, 51): | |
print (k) | |
#Functions | |
#Return | |
def square(import_num): | |
output_num = import_num ** 2 | |
return output_num | |
def get_name(): | |
first = input("first name:") | |
last = input("last name:") | |
return first, last | |
#call | |
first_name, last_name = get_name() | |
#Void no return | |
def printDottedLine(): | |
print(".....") | |
#Files | |
filename = open("hostdata.txt", "<filemode>") | |
#Read from file store to variable | |
file_contents = filename.read() | |
#Readline not by number but after each is called | |
file_line = filename.readline() | |
#Write to File | |
# "\n" is used for new line | |
filename.write('This is put on the file') | |
#Read each line with for loop | |
for line in filename: | |
#Close File | |
filename.close() | |
#Try and except | |
try: | |
print ("trying this") | |
# <tries this if fails goes to except> | |
except: #Type of exception | |
print("exception") | |
#<does this if exception> | |
#CH9 | |
#Dictionaires {key : value} | |
#Create | |
dictonary_name = {'answer' : 42, 'key2' : 56} | |
#Retrive key value | |
d['answer'] | |
#Add / Set a key value | |
d['answer'] = 52 | |
#Delete a key | |
del d['answer'] | |
#Merge two | |
z = {**x, **y} | |
#9.2 Sets | |
#create set | |
#set only contains one of the same thing | |
set_name = set() | |
#Passing a string will split the string into each letter into a element | |
set_name = set('abc') | |
#Will return the set "a", "b", "c" | |
#Must create a list to make the strings a element them self, set will only take one argument | |
set_name = set(['one', 'two', 'three']) | |
#Set will be one, two, three | |
#Add element to set | |
set_name.add("f") | |
#Add list string or another set etc to a set | |
set_name.update(["four", "five"]) | |
#Remove from a set | |
#remove method raises exception when element of set doesnt exsist | |
#discard doesn't | |
set_name.remove("f") | |
set_name.discard("twenty")\ | |
#Find Closest number in a list or set to a given number | |
min(givenList, key=lambda x:abs(x-GivenNumber)) | |
#CH10 OOP | |
#Some documentation https://www.w3schools.com/python/python_classes.asp | |
#Create Class | |
class Person: | |
#Init function always when class is being used | |
def __init__(self, name, age): | |
self.name = name | |
self.age = age | |
def printName(self): | |
print("Hello my name is " + self.name) | |
#Using that class | |
p1 = Person("John", 36) | |
print(p1.name) | |
print(p1.age) | |
#Call function in class | |
p1.printName() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment