Variables

Number/Float

my_variable = 10

Boolean

my_bool = True

Function   

def test():
  var1 = 10
  return var1

Strings

name = "Nikko"

 

Print

Print the output of a function

print function()

Print a string

print "string"

 

Math

Addition

addition = 23 + 25

Subtraction

subtraction = 32 - 6

Muliplication

multiplication = 4 * .75

Division

division = 40 / 4

Exponents

eight = 2 ** 3

Modulo (remainder from a division)

modulo = 3 % 2

Strings

Concatenate multiple strings

print "this " + "is " + "a " + "concatenated " + "string."

Convert numbers to strings

print "I am" + str(25) + "years old"

String formatting with variables (%)

var1 = "Meow"

var2 = "Woof"

print "A cat says %s. A dog says %s" % (var1, var2)

Grab a letter from a string

third_letter = "STRING"[2]

 

String Methods

Length of a string

string1 = "meow"

len(string1)

Convert string to lower case

string1 = "WoWzA"
print string1.lower()

OR

"StRinG".lower()

Convert string to upper case

string1 = "WoWzA"
print string1.upper()

OR

"StRinG".upper()

 

Comments/Special Characters

Comment a line

# This is a comment

Comment multiple lines

""" This is a multi-line comment

"""

Escape special characters

print "That\'s  how you escape those pesky apostrophes"

 

Leave a Reply

Your email address will not be published. Required fields are marked *