Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision | |||
python:python_snippets:w3_schools_python [2024/05/14 04:41] – jcorona | python:python_snippets:w3_schools_python [Unknown date] (current) – removed - external edit (Unknown date) 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== W3 Schools Python Course ====== | ||
- | <code python> | ||
- | """ | ||
- | 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(" | ||
- | else: | ||
- | print(" | ||
- | |||
- | |||
- | #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:// | ||
- | Tomorrow | ||
- | |||
- | """ | ||
- | |||
- | |||
- | #Many named variables, one of after the other. | ||
- | |||
- | name1, | ||
- | |||
- | print(name1, | ||
- | |||
- | |||
- | #One string assigned to multiple variables. | ||
- | |||
- | x=y=z= " | ||
- | |||
- | print(x, | ||
- | |||
- | #Unpacking a collection | ||
- | |||
- | fruits = [" | ||
- | |||
- | x,y,z = fruits | ||
- | |||
- | print(x, | ||
- | |||
- | |||
- | # | ||
- | |||
- | x = " | ||
- | y = "is " | ||
- | z = " | ||
- | |||
- | print(x + y + z) | ||
- | |||
- | #printing two types together, like int and str | ||
- | |||
- | x = " | ||
- | 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 = " | ||
- | |||
- | def variabletest(): | ||
- | x = " | ||
- | print(" | ||
- | |||
- | |||
- | print(" | ||
- | |||
- | 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(" | ||
- | print(a) | ||
- | |||
- | a = int(50) | ||
- | print(a) | ||
- | |||
- | a = float(20.6) | ||
- | print(a) | ||
- | |||
- | a = complex(1j) | ||
- | print(a) | ||
- | |||
- | a = list[" | ||
- | print(a) | ||
- | |||
- | a = (' | ||
- | print(a) | ||
- | |||
- | a = range(7) | ||
- | print(a) | ||
- | |||
- | a = {" | ||
- | print(a) | ||
- | |||
- | a = {" | ||
- | print(a) | ||
- | |||
- | a = frozenset({" | ||
- | print(a) | ||
- | |||
- | a = bool(True) | ||
- | print (a) | ||
- | |||
- | a = b" | ||
- | 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:// | ||
- | Tomorrow | ||
- | |||
- | """ | ||
- | |||
- | </ | ||
- | |||
- | <code python> | ||
- | |||
- | 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, | ||
- | |||
- | |||
- | #multi-line variables | ||
- | |||
- | multiLineVar = ''' | ||
- | 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(" | ||
- | |||
- | #Use check for a work by using if statement and then print something | ||
- | |||
- | if " | ||
- | print(" | ||
- | |||
- | #Use not in word to check for a word | ||
- | |||
- | if " | ||
- | print(" | ||
- | |||
- | """ | ||
- | End of 5/13 11:24PM | ||
- | Continue | ||
- | https:// | ||
- | Tomorrow | ||
- | |||
- | """ | ||
- | </ |