python:python_snippets:w3_schools_python

This is an old revision of the document!


W3 Schools Python Course

"""
This is me running through a w3 python course to try and get stronger with python
"""
 
#here is a basic if else statement covering greater than and less than.
 
if 5 > 2:
    print ("5 is greater than 2")
 
if 5 < 2:
    print("5 is greater then 2")
else:
    print("2 is less than 5")
 
 
#Here is a complicated if else statement where integers are calculated before being changed to strings to be shown in a text string response
 
x = 5
y = 2
 
print(x)
print(y)
 
a = str(3)
b = int(4)
c = float(5)
 
print(type(a))
print(type(b))
print(type(c))
 
print (a,b,c)
 
d= 'lower case d'
D= 'upper case D'
 
print(d)
print(D)
 
if x > y:
    x = str(x)
    y =str(y)
    print(x + " is greater than "+ y)
 
if x < y:
    print (x + " is greater than "+ y)
else:
    x = str(x)
    y = str(y)
    print(y + " is Less than " + x)
 
 
"""
End of 5/9
Continue 
https://www.w3schools.com/python/python_variables_names.asp
Tomorrow
 
"""
 
 
#Many named variables, one of after the other.
 
name1,name2,name3 = "John","Lisa","jane"
 
print(name1,name2,name3)
 
 
#One string assigned to multiple variables.
 
x=y=z= "banana"
 
print(x,y,z)
 
#Unpacking a collection
 
fruits = ["apple","banana","grapes"]
 
x,y,z = fruits
 
print(x,y,z)
 
 
#concatenating string example
 
x = "Python "
y = "is "
z = "awesome"
 
print(x + y + z)
 
#printing two types together, like int and str
 
x = "python"
y= 5
 
print (x,y)
 
 
#global variables outside of a function can be used by all functions.
#variables inside of a function do not exist outside of the function
 
x = "awesome sauce"
 
def variabletest():
    x = "fantastic"
    print("Python is " + x)
 
 
print("Python is " + x)
 
variabletest()
 
 
#Use the global keyword to make a variable inside of a function usable outside of it.
 
def globalvariabletest():
    global y
    y = "I am a global variable"
    print(y)
 
globalvariabletest()
 
print(y)
 
#Here are the different data types for variables
a = str("hello world")
print(a)
 
a = int(50)
print(a)
 
a = float(20.6)
print(a)
 
a = complex(1j)
print(a)
 
a = list["a","b","c","d"]
print(a)
 
a = ('e','f','g','h')
print(a)
 
a = range(7)
print(a)
 
a = {"name":"jonathan","age":38}
print(a)
 
a = {"apple","banana","pie"}
print(a)
 
a = frozenset({"cherry","pineapple","pen"})
print(a)
 
a = bool(True)
print (a)
 
a = b"beatyourmeet"
print(a)
 
a = bytearray(20)
print(a)
 
a = memoryview(bytes(20))
print(a)
 
 
a = None
print(a)
 
 
"""
End of 5/10 1:19AM
Continue 
https://www.w3schools.com/python/python_numbers.asp
Tomorrow
 
"""

integer = 1
floater = 2.5
complexnumber = 5j
 
print(type(integer))
print(type(floater))
print(type(complexnumber))
 
convinteger = int(floater)
convfloater = float(integer)
convcomplex = complex(integer)
 
 
print(convinteger)
print(convfloater)
print(convcomplex)
 
 
import random
 
print(random.randrange(1,50))
 
 
#multi-line variables
 
multiLineVar = '''I am a masterful magician
I also dont like otters.
Otters are evil.'''
 
print(multiLineVar)
 
#strings are arrays
 
arrayVarTest = '''
We like to eat hot dogs and pizza. 
These things will clog your arteries.
'''
print(arrayVarTest[10])
 
 
#You can iterate through an action by using the for / in statement.
 
forstringintest = "I am a test"
 
for letters in forstringintest:
    print(letters)
 
 
#Find the length of a string
print(len(forstringintest))
 
#Find a word in a string using the IN statement.
print("test" in forstringintest)
 
#Use check for a work by using if statement and then print something
 
if "test" in forstringintest:
    print("The word test is in this word")
 
#Use not in word to check for a word
 
if "banana" not in forstringintest:
    print("THe word banana is not in this word")
 
"""
End of 5/13 11:24PM
Continue 
https://www.w3schools.com/python/python_strings_slicing.asp
Tomorrow
 
"""