Easy Python 3 - Lesson 1

7/16/2020

To Do!

Introduction

Python logo.

There are many ways to learn Python. Most of them begin with a "getting started" tutorial showing how to install the Python software. This is followed by information on how to use that software and after jumping through a few hoops you may, within about 60 minutes or so, write your first program, usually called, "Hello, world!"

Hello, world! - 5 ways

We'll eventually get to doing all that. But now, for something completely different, we'll go to a website and write the "Hello, world!" program within about the next 60 seconds.

Hello, world!

1. Click here.

2. In the Code  pane type: print("Hello, world!")

3. Click  Run 

VoilĂ ! Easy as 1, 2, 3.

Congratulations! You have just completed a time honored tradition by writing a program that has been written in hundreds of languages by many thousands of programmers over the past 50 years. Notice when you clicked the Run button that the Output  pane displayed the text Hello, world!

Let's keep going.        We'll learn more about using CodeSkulptor along the way.

Hello, world! - with a variable

1. Click the  Reset  button to reset the Output pane to empty.

2. Put a new line at the top of the Code pane and type message="Hello, world!".

3. In line 2, change the inside of the parenthesis to message without the quotes.
Your code should look like this:

message="Hello, world!"
print(message)

Click Run and notice that the output is the same as the previous Hello, world! code. Why is this?

Note: When introducing a programming concept, look for words in bold. The definition will appear in a tooltip when holding your cursor over the word the first time it is used. For more complete definitions see the glossary at python.org.

In the code above, the first line creates a variable A location in the computer's memory that you assign a name. named message and assigns a value of "Hello, world!" to it. The = symbol in python is used as an assignment operator A symbol that defines a math operation.. In an assignment statement the value on the right side of the = symbol is assigned to the variable on the left side of the = symbol. The series of symbols we see as "Hello, world!" is called a string A series of Unicode characters.. For now, let's just say that a string is a series of symbols or characters. When typing a string, it must be inside quotes and is called a string literal To specify the contents of a string. . You can use either single or double quotes, but whatever you open the string with you must also use to close it.

A quick word about naming variables. You make them to hold data. So, the names you choose should often (but not always) reflect their contents. Here are a few rules for naming variables.

  • A variable name must start with a letter or the underscore character
  • A variable name cannot start with a number
  • A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
  • Variable names are case-sensitive (age, Age and AGE are three different variables)

Let's do another.        Click the  New URL  button and CodeSkulptor will give you a brand new URL of your very own that you can keep forever.

Hello, world! - from an input

1. Click Reset to clear the output pane.

2. In the first line after the = sign, type: input("Say something to the world.")
Your code should look like this:

message=input("Say something to the world!")
print(message)

3. When you click Run, you will be prompted to say something to the world. Type Hello, world! (No quotes needed)

If you typed Hello, world! at the prompt A computer window that waits for a user response. , your Output pane should read the same as the previous two times. This time we used a function A named block of reusable code that performs a task. called input(). We have also been using the print() function. A function always has parenthesis, which can contain parameters Variables inside the parenthesis of a function. . In the code we have written so far, we have used the string, "Hello, world!" and the variable named message as arguments to fill the parameters for those functions.

How about another one?         After you have a new CodeSkulptor URL, you can click  Save   and CodeSkulptor will append your URL with a number. This allows you to save versions of your code.

Hello, world! - using concatenation

1. Change the input() prompt in line 1 to "Just say Hello."

2. In the print() function's parameter, after the variable named message, type: +", world!"
Your code should look like this:

message=input("Just say Hello.")
print(message+", world!")

3. Click Run and at the prompt, just type Hello.

Your Hello, world! should look exactly the same as it has the previous three runs. This time we used concatenation A fancy word for join. to join a string variable and a string literal to make the Hello, world! sentence.

Let's keep going!         To manage your CodeSkulptor work, create a Python folder in your Favorites Bookmark Bar. You can then drag your URL's to that folder and rename them as necessary. To save a hard copy of your Python file (.py) on your hard drive, click  Download 

Hello, world! - using escape characters

1. To the current code, simply add \n in front of the word world in line 2.
Your code should look like this:

message=input("Just say Hello.")
print(message+", \nworld!")

2. Click Run and type Hello at the prompt.

Here we have used the escape Insert characters that are illegal in a string. character \ to insert a new line into the string. Some characters cause the string to break. For example, if you insert a quote character, Python thinks you have ended the string. The most useful characters that need to be escaped in strings are:

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

Lesson Walk-through

Click here for all the code in this lesson.


  Answer each Quick Check question then click the sentence text to see the answer.

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

A location in the computer's memory that you assign a name is called a ___________.

variable

A __________ is a named block of reusable code that performs a task.

function

Variables inside a function's parentheses are called ____________.

parameters

Concatenation is a fancy word for ______.

join

In order to use illegal characters in a string you must use the ________ character.

escape

  Based on what you have learned, write a solution to each problem below. Click the problem text to see my solutions. My answers are only example solutions.

On Your Own

Write a program that imitates a fast food drive in experience. Follow the prompts to order a hamburger, say "yes" to fries and add a drink of your choice. The fast-food employee should repeat back your order.

item1=input("Hello! What can I get for you today?")
input("Would you like fries with that?")
item2="fries"
item3=input("What would you like to drink?")
print("OK! That's a "+item1+" with an order of "+item2+" and a medium "+item3+".")
print("Thank you!")

Using only the keys on your keyboard, create an emoji of some kind. It can be an animal, and object or a face. It must render in the output pane as at least 3 lines, but it can only use the one print function and one line of code. You will need to use the escape characters necessary to write this.

print("\t(\\__/)\n\t(=`.`)\n\t(_(\")(\")")

Write a simple Mad Libs game where you insert the answers to questions into a silly story. Use at least 4 items - an exclamation, an adverb, a noun, and an adjective. Then fit them into a nonsensical story.

print("***Silly Story***")
excl=input("Give me an exclamatory word.")
adv=input("Give me an adverb.")
noun=input("Give me a noun.")
adj=input("Give me an adjective.")
print("All together we shouted "+excl+" "+adv+" as we \nlooked at the "+noun+" with its "+adj+" eyes.")

Acknowledgments



 

This article is a lesson from the high school course Introduction to Computer Science with Python 3. Thanks for viewing. Please check out my other articles on education and technology. I welcome any comments!


Comments »

© 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.