This is a mini-lesson of all of the content gone over thusfar, including:
- Python
- Javascript
- HTML
- Bash
Intro to Python
Python is a very versatile and simple language that is very good for beginners to programming to use.
In this section, we will go over basic python code, and how it can be used, including code for a quiz at the bottom of the section.
Variables:
Defining a variable
When using python, before you use a variable in any function, you must define the variable. The four main variables are:
Strings: Hold text, in quotations. ex: str = "Hi"
Integers: Hold numbers, ex. int = 1
Booleans: Hold True/False statements. bool = False *This is case sensitive!
Lists: Holds groups of text, ex. ls = ["hi", "this is a list"]
Using a variable
Variables can be used in functions to output data into the terminal
For example:
var = "hEy"
print(var)
lower = var.lower()
print(lower)
hEy hey
Functions:
print()
The print function is used to print out text, or a variable to the terminal.
ex: print("Hello World!") when run would print Hello World! into the terminal
input()
The input function can be used to take user input from the terminal
ex: i = input("What is your name?"\nl) would prompt the user to input a value, after printing "What is your name?", then store the input in the "i" variable.
Conditions If and Else:
If and Else conditions are very helpful in restricting certain code to certain conditions, and can be used in tandem with the input() function.
ex. i = input("hi")
if i = "hello!":
print("thanks for responding!")
else:
print("goodbye")
This code asks for an input from the user, and if that input is a certain value, in this case "hello!", the code will print out a response, and if not, it will print out a different response.
Putting it together: If you use all of what has been gone over so far along with some math and one new function, you can create a code for a quiz! This is an example of a quiz I created with this knowledge:
import math
score=0
n="What is your name?\n"
name=input(n)
print(n+name)
print(f"Hello {name}")
print("Please answer all the questions to the best of your ability!\n")
qq="What days of the week do we start live reviews?\nResponse: "
q1=input(qq)
print(qq+q1)
if q1.lower() in ["wednesday", "wednesdays"]:
score+=1
print(f"{q1} is the correct answer!")
qq="What is the full name of this class?\nResponse: "
q2=input(qq)
print(qq+q2)
if q2.lower() == "computer science principles":
score+=1
print(f"{q2} is the correct answer!")
if score < 1:
x = " :("
if score > 1:
x = "!"
finalScore=round(100*score/2, 2)
print(f"{name}, your score is: {score}/2, or {finalScore} percent {x}")
What is your name? Matthew Wang Hello Matthew Wang Please answer all the questions to the best of your ability! What days of the week do we start live reviews? Response: Wednesdays Wednesdays is the correct answer! What is the full name of this class? Response: Computer Science Principles Computer Science Principles is the correct answer! Matthew Wang, your score is: 2/2, or 100.0 percent !
Intro to JavaScript
Javascript is mainly used in html code, in between tags.
Variables:
Defining variables in JavaScript is very similar to in Python, the only difference being is that you use var x = instead of just defining the variable with x = .
ex. var x = "heyyo!"
Functions:
In Javascript, like python, you can create your own functions, or use built in functions. One of the more prominent functions you will use while working with a HTML website is:
document.getElementsById/Name().
This function gets an element on an HTML site, and can be used to edit that element.
For example, if there is an element with id "button" on the site, you can:
<button id='button' value=""></button>
<script>
var x = "input";
document.getElementsById('button').innerHTML = x;
</script>
Some examples of JS code:
Snake
Cps test
Calculator
p.s. end all Javascript code with ;
Intro to HTML
HTML is used for web-development, as is JavaScript.
Tags: Tags are used like functions are in Python and Javascript. They start with "< a tag >", and end with "< /a tag >"
:
is used to define a paragraph. You can put text in these
<span>:
<span> can be used similarly to <p> but has more customization opportunities
<button>:
<button> is used to create buttons which can be pressed and interacted with on the website
<script>:
<script> contains JavaScript code which can interact with the HTML code
<a>:
<a> is used to create links/hyperlinks
<section>:
<section> is used to define functions in a website, which can be hyperlinked to
<ul>, and <li>:
These are used to create lists, which are automatically bulleted
<img>:
Used to input images with a link, or file path
<body>:
Defines main HTML part
<h1>:
Used to create headings
<u>, <b>, etc:
Many tags in html can be used to format text, u is for underline, and b is for bold
<font>:
This tag is used to format text in a larger section
Formatting: Many, if not all of the html tags include formatting. You can change sizes, colors, and alignment, along with most anything you could think of.
Intro to Bash
Bash is used in the Linux terminal, and is mainly used for interaction with files, and occasionally installations and other things.
Main Commands:
cd:
Change directory, can change to the folder path that you provide
ex. cd /matth/project/assets
ls:
Lists all the files in the directory
echo:
prints string/text data into the terminal, or can print commands
ex. echo "hello" would put hello into the terminal, or echo cd /matth/projects/assets would execute the command
cat:
prints the text contents of a file
ex. cd /matth/projects/assets
cat data.txt
-> data data woo data
%%script bash
echo "Installed tools and versions:"
pip --version
brew --version
ruby -v
jupyter --version
echo "Switch to project directory"
echo""
cd /mnt/c/Users/matth/matthew-wang/
echo "List files and folders in project directory"
echo""
ls
echo "print text content of _config.yml"
echo""
cat _config.yml
Installed tools and versions: pip 22.0.2 from /usr/lib/python3/dist-packages/pip (python 3.10) Homebrew 4.1.6 ruby 3.0.2p107 (2021-07-07 revision 0db68f0233) [x86_64-linux-gnu] Selected Jupyter core packages... IPython : 7.31.1 ipykernel : 6.7.0 ipywidgets : 6.0.0 jupyter_client : 7.1.2 jupyter_core : 4.9.1 jupyter_server : not installed jupyterlab : not installed nbclient : 0.5.6 nbconvert : 7.8.0 nbformat : 5.9.2 notebook : 6.4.8 qtconsole : not installed traitlets : 5.1.1 Switch to project directory List files and folders in project directory Gemfile Gemfile.lock LICENSE Makefile README.md _config.yml _data _includes _layouts _notebooks _posts _site csa.md csp.md csse.md images index.md indexBlogs.md resources.md scripts testytesty.md print text content of _config.yml title: CompSci Blogs description: "August 2023 to June 2024" owner_name: Matthew Wang github_username: 7mwang baseurl: "/matthew-wang" remote_theme: pages-themes/hacker@v0.2.0 # remote_theme: pages-themes/dinky@v0.2.0 # remote_theme: pages-themes/minimal@v0.2.0 # remote_theme: pages-themes/hacker@v0.2.0 # remote_theme: pages-themes/cayman@v0.2.0 # remote_theme: pages-themes/time-machine@v0.2.0 plugins: - jekyll-remote-theme future: true