Beginner Python Documentation

The following information is introductory knowledge on coding in python that has been copied over from the Easy Python Docs website. This website is simply to show my current knowlede of HTML/CSS to potential employers. All HTML/CSS coding is stricly my own although the knowledge of python coding is taken from the aforementioned website. If you are looking to begin coding in python I do highly reccomend you check it out it is a very useful site.

Output

Definition

Output in Python is carried out using the print statement. print('text here') will output text here to the screen.

Note
Don’t use a space with the print statment. It should be written as print() not print ().

Easy example

print('Hello world')

Syntax

print(string);

Examples

Example 1 - Output a string

print('Hello')

Example 2 - Output a variable

name = 'Smith'
print(name)

Example 3 - Output variable and string

name = 'Laura'
print('Hello' + name)

Example 4 - Output using a new line character

print('This text is on the first line \n and this text is on the second line')

Key points

Warning
The print statement should always be given a string to print. If you get an error on this statement make sure that you’re not trying to print a different data type, such as an integer - this is especially important if you are concatenating values together.
Hint
You can use print() to simply print a blank line.
Note
Don’t use a space with the print statment. It should be written as print() not print ().
Note
A sequence in programming is instructions which are run one after another in order. It is the most basic programming construct. Example 3 shows a very simple sequence with two instructions.
Input

Definition

Input in Python is carried out using the input statement. When used in a program, this will let the user input some text. This text can then be assigned to a variable.

Warning
If you write input('enter your name: ') then it will ask the user to enter their name, but there name will not be saved to use later in the program. Instead use
name = input('Please enter your name').

Easy example

name = input('Enter your name: ')

Whatever text is entered by the user is assigned to the variable name.

Syntax

variableName = input(string)

Examples

Example 1 - Input to pause a program

input()

Example 2 - Input Statement which also outputs text

input('Press return to continue ...')

Example 3 - Storing user input in a variable

name = input('What is your name? ')
print('Hello' + name)

Keypoints

Note
Don't use a space with the input statement. It should be written as input() not input ()
Hint
name = input('Enter your name') Will not have a space before they enter their name. Instead use name = input('Enter your name ').
Comments

Definition

Comments are part of your code that is for humans not computers. They allow you to make notes about what a line or section of computer code does.

Hint
Comments should be helpful and describe algorithms or difficult to understand parts of a program. It is often more useful to comment a section of code than each individual line.

Easy example

# This is a comment

Syntax

# Put the comment after a hash symbol

Examples

Example 1- A comment on its own

# This is a comment

Example 2 - A comment at the end of a line of code

radius = 10
area = 3.14 * radius #We don't need an acurate value for pi here.

Example 3 - A comment on multiple lines of code

# The following algorithm will
# calculate the distance between
# the two points on the map.

Example 4 - A comment on multiple lines of code using quotes

```The following algorithm will
calculate the distance between
the two points on the map```

Keypoints

Note
Comments are important both for other programmers to understand your code and also for you to understand it. They make it easier for you or someone else to maintain your code. The computer will ignore these comments.
Hint
Python multi-line comments should have a # at the start of each line. However, you can start and end a multi-line comment with triple-quotes, like this: '''. These only need to be put at the beginning and end of the section.
Variables

Definition

Variable are areas in RAM where we can store one value. Each variable has a variable name (also known as an identifier). We use the variable name to refer to the value stored in the variable.

Note
A value could be a string, integer, floating point number or Boolean. For example, 'Adam', 5, 11.89 or True.

Easy example

name = 'smith'

Syntax

variableName = value

Examples

Example 1 - A variable to store a number

age = 15

Example 2 - A variable to store a name

playerName = 'Adam'

Example 3 - A variable to store the current state of a game

gameOver = False

Example 4 - A variable to store the result of a calculation

base - 20
height = 15
triangleArea = (1/2) * base * height
print(triangleArea)

Key points

Note
The = symbol is know as the assignment operator. This is not the same as an equal symbol.
Warning
The value on the right of the = gets stored in the variable on the left. So 'Smith' = name will not work. The error that will appear is SyntaxError: can't assign to literal
Danger
You cannot use spaces in a variable name. For example, player name = 'Smith' will not work. The error that will appear is SyntaxError: invalid syntax
Hint
You can use two words for a variable name joining them together. For example, playerName = 'Smith' This is known as camel case.
Note
Python doesn’t have constants. For constants, simply make a variable and don’t change it!
Strings operations

Definition

A string is a list of characters. We often want to make changes to strings - this is called string manipulation. The most important string manipulation is the ability to join two strings together - this is known as concatenation.

Note
A strings is a list of characters, so we can manipulate them just like lists.

Easy example

a = 'good'
b = 'morning'
greeting = a + b
print(greeting)

Syntax

string1 + string2

Examples

Example 1 - String Assignment

name = input('What is your name? ')
print(name)

Example 2 - String concatenation

subject = 'Mrs Smith'
verb = 'runs'
object = 'home'

sentence = subject + verb + object
print(sentence)

sentenceWithSpaces = subject + ' ' + verb + ' ' + object
print(sentenceWhithSpaces)

Example 3 - Length of a string

lengthOfSentence = len('This is a sentence')
print(lengthOfSentence)

Example 4 - Output strings and numbers

age = 15
name = 'Amy'
height = 159.3

print('Hi ' + name + ', your are age ' + str(age) + ' and ' + str(height) + ' cm tall.')

Example 5 - Get the character in the nth position in the string

sentence = 'My Python program'
a = sentence[0]
b = sentence[5]

print(a)
print(b)
Note
The characters in a string are numbered starting at 0 (zero).

Example 6 - Looping through a string (method 1)

name = 'hello'

for letter in name:
print(letter)

Example 7 - Looping through a string (method 2)

name = 'hello'

for i in range(len(name)):
print('Character number ' + str(i) + ' is: ' + name[i])

Example 8 - Strip (remove whitespace characters)

sentence = ' this is some text. '
sentenceStripped = sentence.strip()
print(sentence)
print(sentenceStripped)

Example 9 -Split (split a string into smaller strings)

gameDetails = 'pacman,1980,Japan,Namco'

listOfDetails = gameDetails.split(',')

gameName = listOfDetails[0]
year = listOfDetails[1]
country = listOfDetails[2]
company = listOfDetails[3]

print(listOfDetails)

print(gameName + ' was created in ' + year + ' and made in ' + country + ' by ' + company)

Example 10 - Lower (to convert characters to lowercase)

name = 'Jacob SMITH'

nameInLowercase = name.lower()
print(nameInLowercase)

Example 11 - Upper (testing an input)

name = 'sMith'

if name.upper() == 'SMITH':
print('Your name is SMITH')

Example 12 - Isnumeric (find if a string contains digits only)

a = 'hello456'
b = '456'
c = '456.23
d = '-23'

print(a.isdigit())
print(b.isdigit())
print(c.isdigit())
print(d.isdigit())

Example 13 - Find left character of a string (slice)

phrase = 'hello everyone'
print(phrase[:7]) #Find first seven characters

Example 14 - Find right character of a string (slice)

phrase = 'hello everyone'
print(phrase[7:]) #Find last seven characters

Example 15 - Find substring between twoo points (slice)

phrase = 'hello everyone'
print(phrase[2:8])

Key Points

Note
The first letter of a string is in position 0. This is the same as any list which starts at 0.
Arithmetic and mathematics
Comparison and logical operators
Data types and conversions
For loops
If statements
While loops
Lists Arrays
Procedures
Functions
File handling
Random numbers
Validation
SQL
Extras