0% found this document useful (0 votes)
63 views

Worksheet 01 - Introduction To Python

The document provides an introduction to Python programming. It discusses choosing Python due to its relative ease of understanding, cross-platform usage, availability of online coding environments and libraries. The document then guides the reader through four tasks: 1) Printing "Hello World" online; 2) A name concatenation program that joins first and last names; 3) A similar program that accepts user input for names; 4) Extending the name program to also accept a middle initial. It notes the difference between strings and integers in Python.

Uploaded by

Kyle
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views

Worksheet 01 - Introduction To Python

The document provides an introduction to Python programming. It discusses choosing Python due to its relative ease of understanding, cross-platform usage, availability of online coding environments and libraries. The document then guides the reader through four tasks: 1) Printing "Hello World" online; 2) A name concatenation program that joins first and last names; 3) A similar program that accepts user input for names; 4) Extending the name program to also accept a middle initial. It notes the difference between strings and integers in Python.

Uploaded by

Kyle
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Worksheet 01 – Introduction to Python

Full name: Aim: Try


some Python
programming.

As you have learned, there are many programming languages out there. We are going to have a look
at some Python programming code. The reasons for choosing this language include:

1. It is relatively easy to understand – there are not too many fiddly rules to remember.
2. Python is a cross-platform language. It can be used on many devices and operating systems.
3. You can create and test some simple scripts using the website
4. There are a large number of libraries available online. These are ready-written programs that carry out common tasks.
5. Python is used by many of the biggest tech companies.

Having said this, programming languages have many similarities. Once you’ve got your head around the basics of programming
it’s much easier to learn new languages as and when you need to.

Task 1 – Getting Started in Python Online


You will be using a website called repl.it. This site allows you to play around with bits of Python code without the need to install
applications. There are limitations to what you can do in repl.it, but it’s a great way to get started.

a. In a new session, type the code print ('Hello, world!') in the coding
window. This is the white window on the left of the screen.

b. Click the Run button (or press ‘Ctrl + Enter’). The Run button briefly
turns to Stop. This can be used to halt programs.

View the
c. Look at the output in the
output from
console. This is the black
the program
window on the right of the
screen.

d. Change the code in the coding window so


that the output displays a different
message when you run the program.

e. Create code so that you get the output


shown on the right. You can use two lines of
code with two print statements.

num_1=3
print("This is a number:","num_1")

Extension
Try achieving this last task with one print statement.
Use \n to create a new line in your text.
Getting Started with Python (page 2)

Task 2 – Displaying a Name


The programming script below takes a first name and a last name, joins them together and then displays the full name. At this
stage, the actual names are fixed in the program.

Assign the name Sarah to the first_name variable.

Assign the name Edwards to the last_name variable.

The two variables are joined together. We say that


the strings (pieces of text) are concatenated.

Display the full name in the console.

Note 1: In Python, a single equals sign (=) assigns a value to a variable. Line 1 could be read as ‘The variable first_name is
assigned the value Sarah’.

Note 2: Python is a case sensitive language. This means that capital letters make a difference to the program. In the
example above, ‘first_name’ would be a different variable to ‘First_Name’. Also, typing ‘Print’ with an upper-case P
would not work. The line spaces make no difference, however. You can add these to make your programs easier to
read. Other spaces (e.g. either side of the + and = symbols) generally don’t have an effect, but spaces and tabs at
the start of a line definitely do have an effect on your Python programs.

a. Type the program into repl.it and run it. Write down the exact output that appears in the console.

____________________________________________________________________________________________________
_

b. We want to add a space between the first name and the last name. We can do this by inserting the code + " " + between
the first_name and last_name variables in line 4, in place of the single + symbol. Edit line 4 and rerun the program.
Carefully write out the new line 4 below.

____________________________________________________________________________________________________
_

Note: The ‘+’ symbol joins two words (or strings) together. We are therefore now joining the first name, a space and the
last name together.

c. A variable is a space in the computer’s memory. Think of it like a box where you can keep information until it’s needed.
Write down the names of the three variables used in this program.

____________________________________________________________________________________________________
_

d. We could make this program more efficient by getting rid of the full_name variable completely. Change line 6 to the code
shown below. Delete the line of code that is no longer required.
e. Enter the title “07.2 Fixed Names” into the name box at the top. Click the Save button.

f. You can access your saved programs at any time by logging in and using the my repls link in the top-right. Your teacher may
also want you to copy and paste the code into a document (with the title above it) for marking purposes.

Getting Started with Python (page 3)

Task 3 – Accepting Inputs in Python


In the previous script, we fixed the first and last names in the code. In real life, it would be more common to ask the user to
enter their name and then do something with the information.

a. Start a new session named “07.3 Input Names”.

b. Try out the code on the right and run it (note


the space after the word ‘Hello’). Type your
first name into the console on the right and
press Enter.

c. You should see an output something like the one shown here
(with your own name, of course).

d. Try the program on the right. This time, you


enter your first name, then your last name.

e. Have a look at the output and check that you


understand what is happening.

f. Make this code more efficient by removing the full_name variable again.
g. Save your program.

Extension for Experts


Try and create this program on a single line, completely removing all the variables. Save the program below.

Task 4 – Scripting Challenge


Copy and paste the code into a new session named
“07.4 Full Name”. Extend it so that it also asks you
for your middle initial and includes this when
concatenating the strings to form your full name.
Our last line is shown below. Yours might be slightly
different.
Extension - Numbers
What happens if you enter numbers into the console rather than names? Is it possible to input two numbers and total them, so
that 1 + 2 = 3 rather than 12? We will look at this in the next task.

Appendix A – ASCII code


HEX is a code, a short-hand way of writing binary. Every HEX symbol represents FOUR bits of binary.
This ASCII table is internationally agreed table for sending messages. For example, a short massage “Hello, today is 8/12/2022”
H e l l o , (space) t o d a y i s 8
0100 0110 0110 0110 0110 0010 0010 0111 0110 0110 0110 0111 0010 0110 0111 0010 0011
1000 0101 1100 1100 1111 1100 0000 0100 1111 0100 0001 1001 0000 1001 0011 0000 1000

When we hit the <ENTER> key on our keyboard, the binary code 0000 1101 is sent from the KB to the computer.
When someone hit the digit ‘8’, the KB sends ‘0011 1000’. This is called THE STRING representation of the digit ‘8’. Its
binary equivalent is 0000 1000.
As strings, ‘8’ + ‘8’ = ‘88’
As binary, 8 + 8 = 16

0 1 2 3 4 5 6 7 8 9 A B C D E F
0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111
A demonstration of the difference between STRING and INTEGER
num_1=input("Enter first digit:")
Who’s done?
num_2=input("Enter second digit:")
Answer: Andrew Nguyen, Tony Nguyen,Eric
print("As strings: ",num_1+num_2) La,Kayla Kim Nguyen, John( Linh) Nguyen,Jessica
(Anh) Ngo,Nathan Do, Daniel Bobadilla, Kevin
print("As numbers: ",int(num_1)+int(num_2))
Hang, Tina lavea, Nyanbuot , Lyna Nguyen, Kathy
Notes: Do, Ray Duong
 the function int(num_1) only works when
num_1 is a series of digits, not characters.
 Use the function float() to handle numbers with
decimal point
 We can’t add + a string and an integer

You might also like