Skip to main content

Outputting, Storing and Representing Data

Welcome to the first real python lesson. Our programs need a way to output information back to users and they also need to store varius data to perform operations on them. We will learn how to do these things in the following lesson.

Lesson Overview

At the end of the lesson you will:

  • Know how to output information to users screen
  • Know what is a variable and how to store values to them
  • Know core python data types:
    • Integer
    • Float
    • String
    • Boolean
  • Know how to represent missing values

Outputting Data Back to User

In Python, we use print function to output some data back to user.

Python Sandbox

Whatever you pass into the function will be outputted to the screen.

We can also print multiple things at once, with python inserting space between each argument.

Python Sandbox

Declaring Variables

You can think of variables as boxes where your program stores various data. Python does not require any special syntax for declaring variables.

Variables are defined by assignment.

my_variable = 5

Declaring variables has some rules:

  • It must start with a letter or underscore
  • It can contain lowercase and uppercase letters, underscores, and numbers

Styling guide PEP8 suggests the use of snake_case for variable naming in python.

tip

See PEP8 - Style Guide for Python Code to learn more about style of writing python code.

Python Core Data Types

Python has 4 core data types with addition of None which is a special value representing no value.

note

Python has a special type() function which returns what type some data is. We will use this to inspect the following data types.

Integer

Integers represent whole numbers. We use them to represent age of user, score of the game and similar things.

In Python, they are represented as int.

Python Sandbox

Float

Floats represent real numbers. We use them to represent prices in shop and various other purposes.

In Python, they are represented as float

Python Sandbox

String

Strings represents text data. We use them to represent names, titles, descriptions and various other purposes.

In Python, they are represented as str and must be wrapped with single (') or double (") quotes.

Python Sandbox

Strings can have special characters called escape character. That character is a single backslash (\). Most common usecase is \n which tells Python that the rest of the string goes on the new line.

Python Sandbox

Boolean

Boolean values represent yes or no state. We use them to represent if something is true or false.

In Python, they are represented as bool and only two valid values are True or False.

Python Sandbox

None

This special value None represents when there is no value. Its type is a special NoneType. Its useful when some of the data is missing or arriving at a later time.

Python Sandbox

Complex, Bytes, Bytearray

Python has a built-in support for complex numbers, as well as for bytes representations, but we will deal with those later.

Type Casting

Type casting refers to transforming one datatype to another. For example, to transform string "32" to integer 32.

Python Sandbox

We can also convert to and from other types.

Python Sandbox
note

If Python cannot convert one type to another, it will raise ValueError. This usually happens when you try to convert some text into number or float.

Error are very helpful as they tell us exactly what is the issue and where they happend. If the errors did not exists, your programs would just silently die, and you would have to randomly guess where the issue is.

We will learn how to read and deal with errors in later lessons.

Excercise

Visit our python-excercise repository and follow instructions there to fork and clone your own copy of excercises repository.

After cloning, try to solve excercise-1 and excercise-2 for better understanding.

Assignment

Try the following excercise to establish your knowlege. We will build a shop, starting with defining (declaring) diffrent variables in this lesson and progress with our little shop as we go further along.

You will need to do this assignment on your own machine.

  1. Create a new directory with the name simple-python-shop
  2. Navigate inside the newly created directory
  3. Instantiate git repository
  4. Create a new file with the name main.py
  5. Inside the file define the the following variables:
    • shop_name - set its value to some string
    • item_name - set its value to some string
    • item_quantity - set its value to some integer
    • item_price - set its value to some float
    • item_available - set its value to some boolean
  6. Output the variables you defined (using print() function) like the following example:
    Shop name: Merlin Shop
    Item name: Excalibur
    Item quantity: 3
    Item price: $67.2
    Item available: True
  7. Make sure your program works as expected then create a repository and push your code to github

Deepen Your Knowlege

Go through these articles to deepen your knowlege about the topics covered in this lesson.

  1. Read more about print() function from Your Guide to Python print Function from this RealPython article.
  2. Learn more about Basic Datatypes in Python from this RealPython article.

What's Next

Now that you have basic knowlege of Python's core data types, we will see how we can use them to make useful output. In the next lesson, we learn about basic math and comparisons.