Python Guide

Python Programming Guide:

Getting Started

Open a new window in python by clicking file then new file.

The first code shown below shows you how to print in python and how to get data from a user. The # lines are comments. Comments are not essential to the code and the code would run without them. The computer knows to ignore these comments but they help you as a programmer as they tell you what the code is doing. You can add comments to a program using # at the start of your sentence. Run the code below to see what it does. Read the comments and try to understand what each line is doing.

 

#printing a line in python.
print("Hello")
#getting data from a user.
name=input("Enter your name: ")
#printing your name
print(name)
#printing your name with additional text
print("Hello",name)
#printing your name with more additional text
print("Hello",name,"how are you?")

 

Notice how brackets are used to tell the computer what to print. Speech marks are used in order to tell the computer what to print as plain text. Variables do not require speech marks. In the program above you have created the variable name. The value of name is whatever text you entered as your name. Everytime the computer see's the word name it now knows name=your name. You have assigned a value to to a variable called name. If speech marks are added to the word name the computer would just print the word 'name' and not its assigned value.

 

We can develop the code further to allow you to also enter your age. However if we want to use age as a number it needs to be formatted as an integer. this is done by using int(age) to change the variable from a string (any combination of numbers or letters) to an integer or whole number.

 

#getting data from a user.
name=input("Enter your name: ")
#printing your name
print("Hello",name,"how old are you?")
#getting a second input.
age=input("Enter your age: ")
#printing out two variables.
print("Ok",name,"Thanks for telling me you are",age)
#performing a process on the data for example adding a year to it.
print("Ok",name,"Thanks for telling me you are",age,"next year you will be",int(age)+1)
#in order to perform a calculation on the age variable you need to format it as an integer.
#an integer is a whole number. int(age) changes the data from a string to an integer.

Tasks:

  • Create a powerpoint called 'beginning programming'. Screenshot the codes you have made so far and paste them into your powerpoint. Add some explanation underneath.
  • Research what data types there are in python other than strings and integers. Find out about float values and booleans etc. Add this information to your powerpoint.
  • Using the programs you have made as a guide create a program of your own that ask the user for their name and age along with several pieces of information such as number of pets, favourite sport, favourite lesson or where they live etc. Your program should then print out the users information onto the screen.
  • Test your program to check it works, sceenshot your program into your powerpoint and add some explanation about what it does.
  • This sort of program is often called a 'chat bot'. See if you can develop your chatbot further.
  • Try to find out how you could ask a yes or no question in your chat bot and have different messages displayed depending on the answer the user gives.