CSC 101 - Day 1

2024-09-07 11:36:09 -0400 EDT


Why Program

In our lives, we are surrounded by computers of various shapes and sizes (laptops, cell phones, etc.), all of which have hardware that is designed to act as personal assistants. This is achieved by programmers add an operating system and software suite that allow the user to make everyday tasks significantly easier. To be able to communicate to these computers and get them to do more specific tasks is a very useful skill. Computers are good at things humans are not and vice versa.

First, lets get into how computers are built.

This Course will cover the programming language Python, and it is your responsibility to understand the grammar and syntax of python as well as understand how to instruct the computer using python to complete your task correctly.

Python has reserved words. These are words that have inherit syntactically meaning and can not be changed. We will be changing words later with variables but variables can not be named any of the reserved words. Here is some of them:

and, as, assert, break, class, continue, def, del, elif, else, except, finally, for, from, global, if, import, in, is, lambda, nonlocal, not, or, pass, raise, return, try, while, with, yield

single quotes and double quotes work the exact same syntactically

Once you’ve download python (which i will not explain; google it), type “python” into your terminal and you will enter interactive mode. You should see something like this:

Python 3.12.5 (main, Aug 9 2024, 08:20:41) [GCC 14.2.1 20240805] on linux Type “help”, “copyright”, “credits” or “license” for more information.

Here you can write or past simple python code and see the output by pressing enter. When you want to leave the shell, type “quit()” to exit. If your code ever has an issue, the python shell will point it out with a an error message, citing the type of error it is and where it was found. Here some of the errors will you will encounter

There are also Logic Errors (mistake in how the statements are used) and Semantic Errors (when the program is executed, but doesn’t do what you want). When you encounter an error, you then begin the process of Debugging. At a beginniner level, it is recommended to go to the 4 Rs:


Python is a High-level language: meaning that the syntax is very far removed from machine code, the language that the CPU uses. But machine code is far too complicated for people to write in, and it depends on machine to machine, so instead we use translators to convert our source code into machine code. This is done either with a Interpreter (converts and executes the code line by line) or a Compiler (converts all the code to be executed later). Python is a Interpreted language

To have Python run a python file (.py) you run “python” and then the file path you want to be executed. Python will automatically stop the program at the end of a file.

Here are some conceptual patterns that are used to construct programs:


Variables, Expressions and Statements

A value is a piece of information that your program can interact with. Values come in different types with different uses. Here are the most common:

It should be noted that python doesn’t comma’s between numbers - won’t take 1,000,000, but will take 1000000.

these values can be stored in variable. you can create variables with assignment statements

greeting = "Hello World"
number = 24
decimal_number = 8.5

These values can then be used to in functions and operations. For example, to display the value in the terminal we use the print function.

greeting = "Hello World"
number = 24
decimal_number = 8.5

print(greeting)
print(number, decimal_number)

Variables can also be redefined throughout the program.

greeting = "Hello World"
print(greeting)

greeting = "Goodbye World"
print(greeting)

Variables can be named whatever you like, provided that they are not a reserved word or start with a number. It is general recommended to make them simple without any shorthand and to use underscores “_” to separate words. This is called Snake_Case and is the standard for python.

There are also Operators. These are symbols that represent common operations like addition and subtraction. the values operators are being applied to are called operands. heres some operators:

Here is a example of how each could be used:

8 + 3 = 11
8 - 3 = 5
8 * 3 = 24
8 ** 3 = 512
8 / 3 = 2.6666666666667
8 // 3 = 2
8 % 3 = 2

An Expression is a combination of values, variable and operators - the examples above are expressions. a variable or value by themselves are also expressions. Expressions in Python also follow mathematical order of operations, so feel free to add parentheses around parts of your expression to get the desired result.

The Addition operator can be used with strings as concatenation. This combines the string into a larger string For example: “hello” + “world = “helloworld”. you can also use multiplication to multiply the string - “Hello”*3 = “HelloHelloHello”

To take an input from the user, the simplest way is to use the input function. This will wait until the user types something until executing the rest of the code.

name = input("What is Your Name:")
print(name)

Note: if you ever want to create a new line in a string, use \n

One of the most useful tools in programming is the Comment. Comments do nothing. They are text in your code that are purely there to add notes. Adding notes throughout your code is a very important step in keeping yourself and your teammates on the same page in understanding a program

# compute the percentage of the hour that has elapsed
percentage = (minute * 100) / 60