Detailed guide for learning to program in Python 3
Detailed guide for learning to program in Python 3
This is a general guide to assist in learning Python 3. Not all the components here are necessary for teaching or learning programming for Edexcel GCSE (9-1) Computer Science, for example Turtle graphics knowledge is not required. Teachers should refer to the specification for the content which will be assessed.
Python data types
Data type
Python
Explanation
Abbreviation
Example
integer int string str
float
float
Boolean bool
A whole number.
A sequence of characters that can include letters, spaces and other characters.
A number with a fractional part. Also known as a real number.
Boolean or logical data that can only have one of two values: True or False.
45 "Have a nice day!" 16.76
True False
Built-in functions
Syntax len() print() type() int()
input("prompt")
Description
Example
Calculates the length of a string.
Displays information on the screen. Displays the type (int, bool, str or float) of a variable or value.
Converts a string or float value into an integer number. Often used in conjunction with the input function, e.g. number = int(input("Please enter the number of T-shirts you require:")) Prompts for input from the user. The data entered is assigned to a variable.
>>> ans=len("my string") >>> ans 9 >>> print("Hello world") >>> ans=7.8 >>> type(ans) >>> ans=7.8 >>> int(ans) 7
>>> reply=input("Enter your name: ") Enter your name: Fred >>> reply 'Fred'
Version 2
Page 1
Built-in functions
Syntax range()
max()
Description
Creates a list of numbers. Often used with the for loop, e.g. range(start number, end number, in steps of). End number is the number after the last number required.
Returns the largest of a set of numbers.
Example
>>> for next in range(1,4):
print(next) 1 2 3
>>>max(12,16, 33) 33
Variables and lists (arrays)
Syntax variableName =
variableName = listName = [value,value,value] listName[index]
listName[row][column]
Description
Example
Assigns a value to a variable.
myString="hello world" myNumber= 89 myAnswer=True
Computes the value of an number= 7 * 8
expression and assigns it to answer= len("this is the
a variable.
age")
Assigns a set of values to a myList=
list.
["apple","oranges",8]
Identifies an element of a list by reference to its position in the list, where index is an integer value starting at 0.
myList[2]
Identifies an element of a nested list (equivalent to a two dimensional array).
myList[0][6]
Nested lists in Python (two-dimensional arrays)
How to... initialise a two-dimensional array
address an element in a two-
Example
rowLength=4 columnLength=6 myArray=[[0 for row in range(rowLength)] for column in range(columnLength)]
[row][column]
Version 2
Page 2
dimensional array
assign a value to an element in a two-dimensional array
print the contents of a twodimensional array, a row at a time
myArray[0][4] = 99 myArray[2][3] = 74
for row in range(rowLength): print(myArray[row])
Version 2
Page 3
Selection constructs
Syntax
if :
if :
else:
if :
elif :
elif :
try:
except:
else:
Description
Example
If is true then commands are executed.
if colour == "green": print("It is safe for you to cross.")
If is true then are executed otherwise are executed
if colour == "green": print("It is safe for your to cross.")
else: print("STOP! It is not safe to cross.")
If is true then are executed, else if is true then are executed, etc.
if answer == 1:
print("You will make a new friend this week.") elif answer == 2:
print("You will do well in your GCSEs.") elif answer == 3:
print("You will find something you thought you'd lost.")
If cause an error, then are executed. If execute successfully, then are executed.
try: ans=numOne/numTwo
except ZeroDivisionError: print("Second number cannot be zero!")
else: print("The answer is: ", ans)
Version 2
Page 4
Iteration constructs
Syntax
Description Example
for variable in :
while :
Executes for a fixed number of times, given by .
Executes ................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related searches
- learning to believe in yourself
- things for couples to do in boston
- program for learning english
- how to program in java
- free websites for learning to read elementary kids
- ideas for things to build in minecraft
- learning to code in unity
- apps for learning to read
- install pip in python 3 8
- how to program in matlab
- objects in python 3 example
- best program for learning chinese