Introduction to Computer Science with Python 3

Lesson 1


 

Course Resources

CodeSkulptor3 Cheat Sheet  
CodeSkulptor3 Website  
How to Think Like a Computer Scientist (link to openbookproject.org)  
Beginners Python Cheat Sheet (download PDF from ehmatthes.github.io/)  
Code.org Video Library  
Computer History Museum  

Note

There are many terms used in the resources and texts that are not covered in detail in these lessons. Definitions required for the lessons in this course will be in the Glossary at the bottom of each lesson page. On first usage of a glossary term in the text, it will be bold, highlighted blue and linked to its definition as shown above. In the glossary, you can click the word to link back to the text.

Lesson Objectives

  1. Computer Science
    • Definition
    • Usage
    • Tools
  2. Language
    • Natural
    • Formal
    • Comparison
  3. Python
    • About
    • Programming Environment
    • First Program
  4. Python Code
    • Functions
    • Print()
    • Strings
  5. Lab
    • Demonstration
    • Practice
    • Assignments

Computer Science

Definition

There are many ways of expressing a definition for computer science. Many high school courses use a definition that states something like, "Computer science is using technology to solve human problems." I'm okay with most of that definition, but I would like to add a couple of words and rephrase that definition slightly. For the purposes of this class, let us say that computer science is using digital computer technology to solve human problems. Digital information is data stored and processed by computers as zeros and ones. We will talk about this in more detail in a later lesson.

Usage

Computer Science is Changing Everything

No matter what field you want to go into, Computer Science is changing that industry. Start learning at http://code.org/ Special thanks to: - Elena Silenok /...

The reason we should add the words digital and computer is because, in the truest sense technology includes human advancement using many tools that have been around for thousands of years, such as the shovel or the wheel and axle. Those are technology but not examples of the kinds of technology we will be discussing here. The second definition is the one we will use in this class because we will be focusing on digital information, which is the information that is input, processed, stored, and output by modern, digital, electronic computers.

In the video, Computer Science is Changing Everything from Code.org, you can see that computer science is an integral part of our everyday lives. It only makes sense that a technology that is so prevalent in a society, should be studied and learned. Many of you may go into career where you are required to have specialized knowledge in chemistry, biology, mathematics or any number of occupations. All those fields plus just about any other you can think of will require some level of computer knowledge. You can play games on your computer or phone. Watch TV or listen to music on the Internet. Take a class, pay your bills, or fill out an application to apply for a job. Doing all those things and using computers in your chosen career, makes you a consumer of computer science. Human beings use computer science to produce a better world for themselves and others. My hope for you is that you use your knowledge for good and move from not just being a consumer of computer science, but being a producer.

Tools

As a 21st Century digital consumer, you are aware that you need some tools in order to consume. You may have a computer, a tablet, or a phone. Those are the big three. But there are many other individual tools and supporting tools such as printers, monitors and TV's, scanners, MP3 players, and the list goes on and on. Ever expanding technologies have produced a new paradigm of connectivity. We used to think of the Internet as a network of connected computers, but now a days more and more things, with embedded chips, have become tools for use in computer science. Now we have the IOT - Internet of Things. Connected cars, refrigerators, street lights, door cameras, even entire homes and buildings can be connected computer tools.

For this course we will put all the tools of computer science into two extremely broad and yet very specific categories: Hardware and software. Hardware is the physical device; anything you can touch. Software is instructions; programs that run on hardware. Each of these categories contain a vast number of topics and we will cover as much as possible. We will begin to learn how to work with hardware and software, what constitutes a computer and how the hardware "understands" what the software is telling it.

Quick Check - Answer the question, then click the question text to see the correct answer.

What is a good definition of Computer Science?

One definition of computer science is, "Using computer technology to solve human problems."

What is digital information?

Computer data stored and processed as a series of the digits 0 and 1.

In computer science, any physical device or anything you can touch is called?

Hardware.

Language

Natural

Humans communicate using language. It should not be surprising then, that possibly our greatest invention, the computer, also communicates with us and other devices and we with it, using language. Speaking and writing are done in natural language. A natural language is one, such as English or Spanish, that evolved naturally. Natural languages allow for ambiguity, vagueness, metaphor and literary device. Rules can be broken when using a natural language yet the meaning is understood by other clues. Natural languages are not strict. Using natural language even a statement as simple as, "I saw a bat" is complicated for a computer. Does it mean saw like seeing or saw like cutting? Does bat mean an animal or a piece of wood? In natural language that ambiguity can be figured out by other means, such as a picture or even further explanation.

Formal

A formal language, on the other hand, is one designed for a specific purpose, such as in mathematics, chemistry, or computer programming. All programming languages are formal languages and therefore have strict rules called syntax that govern their structure. As we learn to program using the formal language of Python, a term we will become ever more familiar with is, algorithm. An algorithm is a set of specific steps for solving a category of problems. When we program in Python, we will be creating algorithms to help us solve problems. The necessity of strict adherence to Python's rules will become evident. The video from CS50, the most popular computer science class at Harvard, shows a couple of ways to make a PB&J sandwich and show the importance of using formal languages in computer programming.

Comparison

natural vs. formal language infograph
Natural vs. Formal - How to Think Like a Computer Scientist

We might say, but can't computers understand English when I do a Google search. There is some truth to that. As an end user, you are seeing the results of programming when you are using Google Search and other software such as Alexa, that use a branch of software development called AI (artificial intelligence) that is programmed to do NLP (natural language processing). But to be sure, the underlying programming language used to write that software is a formal language with strict rules. The parts of a formal language are precise, exact and have a literal meaning. Instructions written in a formal language produce the same result every time.

Quick Check - Answer the question, then click the question text to see the correct answer.

In computer science, English is an example of what language type?

English is a natural language.

An ____________ is set of specific steps for solving a category of problems.

algorithm

Which type of language is concise, literal and exact?

Formal language.

Python

About

We will be using many resources for this course. The main source we will use is, How to Think Like a Computer Scientist: Learning with Python 3 . In this quest to think like a computer scientist, the text tells us that, "The single most important skill for a computer scientist is problem solving." The two primary tools we will use in this endeavor to become problem solvers, are our own minds and the programming language, Python.

Python is a high-level language. High-level programming languages like Python are designed to be easy for humans to read and write. They often use code that is somewhat similar to natural language, in that they have words like print, input, range, else, and if, but use them in a formal way. Languages like C++, PHP, Pascal, C#, and Java are all high-level. These languages are easier for people to use and faster to program. Unlike low-level languages that are designed to be easy for a computer to execute. Low-level languages are also called machine language or assembly language and deal directly with ones and zeros or the somewhat curious assembly commands.

low and high level language examples
Low-level vs. High-level - Video by Banchory Academy

Programming Environment

Another tool that is necessary for using Python is a programming environment, often called an IDE or Integrated Development Environment. An IDE is a single application that includes a set of services for programmers to development software. Usually an IDE will have a minimum ability to edit, debug, and run code. Many full featured IDE's, such as Microsoft Visual Studio have the ability for multiple developers to work on large projects. Usually an IDE will have a minimum ability to edit, debug, run code. The IDE we will use in this course is CodeSkulptor. It was developed at Rice University for teaching beginning Python to college students. I chose it for this course due to its easy to use interface and its availability which make it a good choice for high school students.

First Program

A program is an algorithm that has been coded into instructions that can be run by a computer. The text also gives a pretty good picture of the programs you will be writing with Python.

input Get data from the keyboard, a file, or some other device.
output Display data on the screen or send data to a file or other device.
math Perform basic mathematical operations like addition and multiplication.
conditional execution Check for certain conditions and execute the appropriate sequence of statements.
repetition Perform some action repeatedly, usually with some variation.

As we progress in using Python we will learn even more about the concepts discussed here. But we will begin typing code quickly. Just as with an natural language. such as English, you learn it by speaking it. So it is with a formal language, like Python. You learn it by typing it.

Read to the end of this lesson, watch the lab video, and then go to https://py3.codeskulptor.org and type in the code below to create the "Hello, world" program. This first program is a time honored custom that has been followed by literally thousands of programmers throughout computer history. Follow along with me the video below as we write our first program and I walk through how to use CodeSkulptor for this class.

Caution

It is tempting to use technology to make writing code easier by copying and pasting. Learning any programming language requires actually typing it. Doing the work, helps you remember the commands and proper syntax. It will become easier and more intuitive as you go along. I will say it again: The more your type it, the more you learn it.

Our first program is very simple but we will be adding to it very quickly.

#Name
#Class
#Date

print("Hello, world!")

Quick Check - Answer the question, then click the question text to see the correct answer.

Is Python a high or low level programming language

Python is a high-level programming language.

Software used to edit, debug, and run code is called an _____________.

An IDE or Integrated Development Environment.

Coded instructions that can be run by a computer are called?

programs

Python Code

Functions

Now that you have typed and run the program, let's take a closer look at Hello,World! and the meaning of each item.

  • # - denotes a comment.
  • print() - a Python built-in function that prints output screen.
  • "Hello, world!" - a string literal argument for the print() function.
You can see the first three lines of our program all begin with a # token. The # symbol is used to insert a comment. Comments are used by programmers to help identify what their code does. Comments do not appear in the output of the program. When you insert a comment, everything after the # cannot be seen in the output. A comment can be inserted at the beginning of a line or after the code in a line. Good programmers use comments often. Good commenting makes your code easier read and helps in understanding what it does.

The next line of our program is print() which is a function. A function is a named block of code that is designed to complete a specific task. In some programming languages functions are called procedures or sub-routines. This function is named "print" and we use it by typing its name followed by opening and closing parentheses. After the function's name, there may be one or more parameters, information sent to a function inside the parentheses. When the function is called or used, the parameters receive data, which are the actual values used by the function to complete its task. We will cover this more thoroughly in the next lesson. Python has many built-in functions, many more that can be imported, and you can create your own which makes Python highly extensible.

Strings

Inside the parentheses of our Hello, world! program, is what else, "Hello, world!" in quotes. This is called a string. A string is a series of characters. String characters are Unicode, which comprises over 150,000 characters, but for now we will only be using ASCII characters, which are basically the keys on your keyboard. ASCII, is an acronym that stands for American Standard Code for Information Interchange. It is a character encoding standard for electronic communication. ASCII codes symbolize text in computers, cell phones, and other devices. We are going to talk much more in this course about how computers deal with numbers and other characters. You can learn a little more about ASCII here.

Strings are always enclosed in quotes. The quotes can be either single of double, but they must match. If the string begins with " it must end with ". Strings can be connected with a + sign, which is called string concatenation . Example: "foot" + "ball" renders football. In this example, you could also leave out the plus sign and the strings would still connect but, get in the habit of using the plus sign when doing concatenation because we will often be connecting other items to strings. More on that later.

There are some symbols that Python uses and putting them into a string, causes an error. You must use an escape character when using them. These are characters that would break the string, such as quote marks which are already being used to signify a string. Here are the escape characters:

Escape character Prints as
 \' Single quote
 \" Double quote
 \t Tab
 \n New line
 \\ Backslash

Lab

Demonstration

Practice

  • Open and practice creating, saving and opening files in CodeSkulptor.
  • Practice the print() function with multiple lines and other ASCII characters.
  • Practice concatenation with words, phrases and symbols.

Assignments

  • Create a class folder in your Favorites Bar of your browser.
  • Create a properly formatted Hello, world! program, save it in your class folder and send the URL to me.
  • Create an ASCII emoji or animal, using only your keyboard characters. Send me the CodeSkulptor link.
  • Create the same ASCII figure using only one print() function.

ASCII emoji example:
my ASCII emoji example

Glossary

computer science

Using digital computer technology to solve human problems.

digital

Signals or data expressed as a series of the digits 0 and 1.

hardware

The physical device; anything you can touch.

software

Instructions/programs that run on hardware.

natural language

A language, such as English or Spanish, that evolved naturally. Natural languages allow for ambiguity and vagueness. Rules can be broken when using a natural language yet the meaning is understood by other clues. Natural languages are not strict.

formal language

A language that is designed for a specific purpose, such as in mathematics, chemistry, or computer programming. All programming languages are formal languages and therefore have rules and syntax that cannot be broken without the loss of functionality. Formal languages are very strict.

syntax

A set of rules that govern the structure of a program.

algorithm

A set of specific steps for solving a category of problems.

high-level language

A programming language like Python that is designed to be easy for humans to read and write.

low-level language

A programming language that is designed to be easy for a computer to execute; also called machine language or assembly language.

IDE

Integrated Development Environment: single application that includes a set of services for programmers to development software.

program

An algorithm that has been coded into instructions for a computer.

token

An individual element of a programming language.

Comment

Used by programmers to explain their code. This text is not output by the program.

function

A named block of code that is designed to complete a particular task.

parameters

The information sent to a function inside the parenthesis.

string

A series of Unicode characters.

ASCII

An acronym that stands for American Standard Code for Information Interchange, is a character encoding standard for electronic communication.

string concatenation

String concatenation is the operation of joining character strings end-to-end using the plus sign.

escape character

Escape characters allow the use of characters that would break a string.

Works Cited and Acknowledgments


 



© 2020 - KRobbins.com

If attribution for any resource used on this or any page on krobbins.com is incorrect or missing, please check the about page. If there is still an error, please contact me to correct it.