Python Logo

Python Reference

Variable Names

Variable names cannot have the following:

As good practice you should always descriptive variable names that tell a reader what kind of information a variable holds. We will choose the convetion of using all lowercase letters for variable names.

The print() function.

The print function allows us to output to the console. Usually, you will output strings but you can also output numerical data types and lists.


          phrase = "Work hard!"
          print("Hello World!")
          print(2+4)
          print(phrase)
        

          Hello World!
          6
          Work Hard!
        

Strings

Definition

An immutable sequence of characters wrapped inside single, double or triple quotes.

f-Strings

Strings that are immediately preceeded by an "f" are called f-strings. The "f" stands for "format". f-strings allow us to insert variables into a string when the variable name is enclosed in between curly brackets.


          name = "Eric"
          print(f"My name is {name}.")
        

          My name is Eric.
        

Methods

All strings have the following methods:


          string = "   aUtoboTS TrAnsfOrm!  "
        

Lists

Definition

A list is a collection of data in an ordered list.

Accessing Elements in a List

You can access a specific element by index


          people = ['euler', 'cauchy', 'gauss']
          print(people[1])
        

          cauchy
        

Modifying Elements in a List

You can modify an element on a list by providing its index and reassigning its value: people[1] = 'mobius'

Adding Elements to a List

append()

The append() method adds an element to the very end of a list.


          # Adds Leibnitz to the end of the list
          people.append('Leibnitz')