Python - The Basics

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 6

Python – the basics

Topic: Printing text Output in red


print (“Hello World”) Hello world
print ( 1 * 2) 2
print (6 * 7) 42
print () print blank line
print (“Python’s string are easy to use”)
print (‘We can even include “quotes” in string’)
print (“Hello” + “world”)
Output:
Python’s string are easy to use
We can even include “quotes” in string
Helloworld
Concatenate strings
Print with no space, comment line and print with space
Printing text Output
greeting = “Hello”
name = “John”
print (greeting + name) HelloJohn (print with no space)
# if we want a space, we can add that too (this is the comment line)
this line will not appear in your output. Note the symbol that is
used before the comment line.
print (greeting + ‘ ‘ + name) Hello John (print with space)
You Try It 1
(a) Write the code to print out the string - My favourite sport is
basketball.
(b) Write the code to print the result of the calculation 10 times 8.
These lines of codes will allow you to input the
answers from the keyboard and concatenate
them as strings.
name=input("Please enter your name")
age=int(input("Please enter your age"))
phone=int(input("Please enter phone number"))
space = ‘ ‘
print("My name is" + space + name + space + "I
am" + space + "years" + space + str(age) + space
+ "my phone number is" + space + str(phone))
Continuation - strings
Write lines of codes that allow you to enter your
name from the keyboard. Store strings in variables.

greeting = “Hello”
name = input (“Please enter your name: ”)
print (greeting + ‘ ‘ + name)
Output
Please enter your name: Tommy
Hello Tommy
Print strings over several lines
The forward slash n causes the cursor to start a new line.
Splitstring = “This string has been\nsplit over\nseveral\nlines”
print(Splitstring)
Output:
This string has been
split over
several
lines
The forward slash t causes the cursor to place a tab at the next
tab position.
tabbedString = “1\t2\t3\t4\t5\t”
print(tabbedString)
Output:
1 2 3 4 5
Exercise 1
1. Write a program in python to output the
following:
Number 1 The Larch
Number 2 The horse chestnut
Number 3 The evolution process

2. What will be the output produced by this line?


Terry\nJohn\nGraham\nMichael\nEric\nClive

You might also like