10
Python Programming Language

Python Programming Language. What have we learnt so far? Variables. What are they used for? Data types. What three data types are there? White space

Embed Size (px)

Citation preview

Page 1: Python Programming Language. What have we learnt so far? Variables. What are they used for? Data types. What three data types are there? White space

Python Programming Language

Page 2: Python Programming Language. What have we learnt so far? Variables. What are they used for? Data types. What three data types are there? White space

What have we learnt so far?

• Variables. What are they used for?• Data types. What three data types

are there?• White space. Why is this important?• Statements. How do we write a

statement?• Comments. How do we write a

comment?• Arithmetic What are they? operatorsxx

There is 6 in total.

Page 3: Python Programming Language. What have we learnt so far? Variables. What are they used for? Data types. What three data types are there? White space

Case Sensitivity

• You must be careful when you are typing and use the appropriate letter case for the different syntax you are using.

– Variables are all in lowercase. spam = 6

– Booleans are title case. bool = True

Page 4: Python Programming Language. What have we learnt so far? Variables. What are they used for? Data types. What three data types are there? White space

Strings

• Strings are typed in between two speech marks.• Below is the variable brian that has been assigned the

following string.• “Always look on the bright side of life”

Page 5: Python Programming Language. What have we learnt so far? Variables. What are they used for? Data types. What three data types are there? White space

Escape

• Certain character cannot be just typed into a string, they need to be escaped.

• For example if I’m typing in I’m I need to escape the apostrophe by using the back slash symbol.

• If you used double quotes you can get away without using the escape back slash symbol.

Page 6: Python Programming Language. What have we learnt so far? Variables. What are they used for? Data types. What three data types are there? White space

Access by Offset

• Each character in a string has a subscript or offset, which is a fancy way of saying it has a number attached to it. The number starts at 0 for the leftmost character and increases by one as you move character-by-character to the right. Check out the diagram in the editor!

Page 7: Python Programming Language. What have we learnt so far? Variables. What are they used for? Data types. What three data types are there? White space

Methods

• Methods are pre-built pieces of code that preform certain tasks for us.

• Four String Methods are : - – len() This will give us the length of the string.

• print len (parrot)– lower() This will convert the string to lowercase

• print parrot.lower()– upper() This will convert the string into upercase

• print parrot.upper()– str() This will make strings out of non strings

• print str(parrot)

Page 8: Python Programming Language. What have we learnt so far? Variables. What are they used for? Data types. What three data types are there? White space

Dot Notation… len(string) and str(object)

• Why use len(string) and str(object), but dot notation (e.g."String".upper()) for the rest.

• Dot notation works on string literals ("The Ministry of Silly Walks".upper()) and variables assigned to strings (ministry.upper()) because these methods are specific to strings—that is, they don't work on anything else.

– best teacher = “Mr Snell is brilliant”– mr snell is brilliant”.upper()– best teacher.upper()

• By contrast, len() and str() can work on a whole bunch of different objects so they aren't tied to strings with dot notation

– len (best teacher)

MR SNELL IS BRILLIANT

MR SNELL

21

Page 9: Python Programming Language. What have we learnt so far? Variables. What are they used for? Data types. What three data types are there? White space

Explicit String Conversion

• Str() turns non-strings into strings? The fancy name for that process is explicit string conversion.

• You're explicitly telling Python, "Hey, I know this isn't a string, but I want to turn it into one.“

– Making a number into a string can let you glue together strings and numbers (which Python normally won't allow). 

Page 10: Python Programming Language. What have we learnt so far? Variables. What are they used for? Data types. What three data types are there? White space

String Formatting

• The % string formatter replaces the %s (the "s" is for "string") in our string with the variables in parentheses.

• The syntax went like this:– print "%s" % (string_variable) – You can have as many variables (or strings!)

separated by commas between your parentheses as you like: