Worksheet 01 - Introduction To Python
Worksheet 01 - Introduction To Python
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.
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.
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)
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.
c. You should see an output something like the one shown here
(with your own name, of course).
f. Make this code more efficient by removing the full_name variable again.
g. Save your program.
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