Starting with Python – Basic string & math functions

Below is a quick overview of key Python functions

Multi-Line Comments

“””Here is a multi-line comment

These help you comment out multiple lines at a time

use three speech marks at the beginning and end to create a multi line comment”””

Single line comment

#This is a single line comment. Just use a hashtag at the beginning of any statements

Define Variables

To define variables in Python, simply type the variable name, the equals sign & then define it’s value

MyName = “Kieran”

MyNumber = 10

LikesPie = True

Print values & variables

You can print values on screen through either your variable names or by explicitly stating the value to be printed

Print MyName

Print “Kieran”

Print MyNumber

Print LikesPie

Mathematical Functions

Let’s define some variables to work with:

Number1 = 10

Number2 = 25

Addition Example

AddOneandTwo = Number1 + Number2

Subtraction Example

SubtractOneandTwo = Number1 – Number2

Division Example

DivideOneandTwo = Number1 / Number2

Multiplication Example

MultiplyOneandTwo = Number1 * Number2

Using Powers

We can take this a step further and use powers. For example, using the below, my variable “MyPower” is equal to 8, this is because its value is 2 to the power of 3.

MyPower = 2 ** 3

Calculating Remainders

We can also automatically calculate the remainder from a division. So the below would output 1.

MyRemainder = 3 % 2

String Functions

Escaping Characters

In many cases, we will run into issues when using apostrophes. We will therefore need to escape them from the string, ensuring no issues are encountered. To escape characters, we can use \ just before the apostrophe.

‘There\’s no need to worry about apostrophes’

Finding character X of a string

Now let’s find the fourth letter of my name (starting from position zero)

MyFirstNameLetterFour = “KIERAN”[3]

Calculate string length

Now, let’s find out how long the full string is. The below would output 6

MyFirstName = “Kieran”

Print len(MyFirstName)

Make a string upper / lower case

For consistency, we can make a string all upper or all lower case: 

Print MyFirstName.lower()

Print MyFirstName.upper()

Alter data types

Now, sometimes, we may want to turn an integer into a string, we can do that by using str()

MyAge = 27

Print str(MyAge)

String Concatenation

We can easily concatenate strings using plus signs

FirstName = “Kieran”

LastName = “Keene”

FullName = FirstName + “ “ + LastName + “Age: “ + str(MyAge)

Print FullName

User input & variable concatenation

Now, let’s get a bit interactive. The user can enter variables in the terminal. We can also use %s to replace variables in a string concatenation & rather concatenate all variables at the end of the string, concatenated using %

name = raw_input(“What is your name? “)

school = raw_input(“What is your school? “)

country = raw_input(“What is your country? “)

print “Your name is %s, your school is %s, ” \

“and your country is %s.” % (name, school, country)