Back to Top

About Python

Python is a general-purpose programming language. This means that you can create various types of software in it: write web applications, scripts to improve the operation of the system, analyze large data sets, create artificial intelligence. There are really many applications for Python.

If you do not have any programming experience, Python is a good choice for a start: it's an easy language for beginners, but at the same time, it offers great possibilities. It is designed so that the programmer does not have to "fight the syntax", he only solves problems. Python is used by small, medium and large companies: Google uses this language extensively in its search engine and on YouTube. Instagram and Pinterest are written entirely in Python!

 

Python's history speaks best for its potential and flexibility. When it was created in 1991, many branches of computer science, in which it is most extensively used today, were only starting to develop. Apart from a handful of enthusiasts, nobody seriously thought about them!

About variables

Each program - regardless of the language used - must store the data it will use. This may be, for example, the user's name or the price of the purchased movie DVD. We store data in special constructions which we call variables.

In Python, variables are defined as follows:

# variable declaration
user = "Anakin Skywalker"
price = 30
price_with_vat = price * 1.23

See an example: Example of variables

Let's start with the explanation from the # sign: it indicates a comment and it's for describing the code. Everything you see in the line of code after this sign will not be executed by the computer.

Please note the following:

  • each variable has its own name (user, price, title),

  • there is an equality sign after the name (this is the assignment operator)

  • after the equality sign, the variable value follows.

In the above example, we have defined the following three variables:

  • variable named user, in which the "Anakin Skywalker" (Star Wars FTW!) string is stored,

  • a variable named price, in which the number 30 is stored,

  • at the end we have the variable price_with_vat, which stores the value 36.9 (note that we use the American notation, where the integer part is separated by a period, not a comma!). This value was created by multiplying the value stored in the variable price by 1.23.

 

The simplest way to imagine a variable is a box with a label. We can store different items in the box - in our case, various information. In Python, first we write the name of the "label", and after the equality sign - the contents of the "box". We can replace the contents in the box, e.g.

user = “Darth Vader”

From now on, the variable user stores the value "Darth Vader".

Important note: if you do not declare a variable - you can not use it! When trying to use a variable that is not defined, you will get an error!

About naming variables

Call it as you want. :) Although, as everywhere, Python programmers have certain rules:

  • use letters (remember that Python distinguishes between uppercase and lowercase letters : A is something completely different than a!)

  • you can use digits (but the variable name can not start with a digit),

  • the only character that is not a letter or digit, and at the same time is allowed in the variable name, is the underscore sign. 

 

About data types

Variables can store different data. The most popular are:

STRING

# variable name stores a string with the name, e.g.
name = “Anakin”

Note that the string is stored in quotes.

NUMBER

A number can be an integer (abbreviated as int) or a floating point number (float)

# int type variable named age stores the user’s age
      age = 45
 
# float type variable stores the user’s height 
      height = 1.88

LOGICAL VALUE (BOOLEAN)

It can take two values:

  • True
  • False

(both values must necessarily be capitalized).

# logged_in logical variable stores the information
# whether the user is logged in or not:
# (True - yes, False - no)
logged_in = True

NONE (NOTHING)

Sometimes we want the variable value to be empty, just like a box can be empty. Zero is not a solution here, because zero is an int value. This value is None (also capitalized).

# variable with an undefined value
# stores the knowledge of Jon Snow
jon_snow_knows = None

About communication with the user

The data exists to inform the user. We can therefore display them on the screen with the print() statement.

# An example so popular that everyone knows it and does not need to be explained.
# Just pay attention to the brackets: they are obligatory!
print("Hello World!")
 
# and here we display the age of Darth Vader
# defined in the examples at the beginning of the lesson
print(age)
 
# and now something more difficult - a combined message:
# we display many variables and even strings that are not
# placed in any variable. Pay attention to commas
# separating successive values.
print(name, "is", age, "years old.")

Task 1

Define a product variable, assign the value "Computer" to it. Then define a net_price variable - assign the value of 1500 to it. Display a message on the screen with the name and the net price of the product.

Solve the task here: Task 1

Need a hint?

See solution: Solution to Task 1

Task 2

Look at the code written in task 2. Why doesn't it work? Correct it and run it. Read the error message that will be displayed carefully, you will find instructions there.

If you are still having problems, see task 1.

Need a hint?

See solution: solution to problem 2

Task 3

Look at the task code of the third task. There are four variables of the logical type and one numeric variable. Each of them corresponds to a particular kind of weather. Look out the window, check the weather and set the variables accordingly. Run the program.