Чтение онлайн

на главную - закладки

Жанры

Python Handbook For Beginners
Шрифт:

2) Second, we put equal sign =

3) Third, we give a value to our variable "Super Cat."

A value in a variable always goes with quotes, like in our example. Otherwise, a variable will not work.

Now, as we know what a variable is and what it contains, let's make one!

2 How to Create a Variable?

Write our variable from the example above to the console:

film = "Super Cat"

And run the code. Nothing? That's right. There is no message to show on the screen as we did not use the print function. We will combine variables with print function in the following chapters. But, for now, hit the >_REPL button.

You should now see the input field that looks like this:

Input the name of our variable, which is the film, and hit enter.

In response to the variable name, the program returns its value, which in our case is "Super Cat."

Now, as you've created your first variable, experiment with it. Change name and value. See what happens.

3 Let's Print Variable Value

We've just learned how to create a variable. Now let's learn how to output its value.

Do you remember the print function we used earlier? Let's combine it with our variable and see what happens. Write this code in the console and hit run:

film = "Super Cat"

print(film)

Let's break it down:

1) First, we declared a variable and gave it a name – film.

2) Then we assigned a value to the variable – Super Cat.

3) Then, on a second line, we wrote a print function.

And finally, we passed the variable name to the print function, putting it in the function brackets.

Every time we create a variable and pass its name to the print function, it will output the variable's value, just like in our example.

Now you know how programmers display variables in Python. It doesn't seem too tricky. Let's have some practice. Below is the code that is missing some elements. You have to fix it so the program could create a variable and display its value.

= " "

By now, you should have sufficient knowledge and skills to accomplish this task. When done, iterate on it as long as you like. Change variable names and values. Hone on your new skills!

4 Wrapping Up Chapter Two

In chapter two, we have accomplished the following:

1) Learned what a variable is;

2) Created our first variable;

3) Learned how to display variable values.

5 Chapter Two Test

1. What is the purpose of variables?

1) Computers use variables to store information.

2) Computers use variables to change information.

3) Computers use variables to retrieve or delete information.

2. If a variable name consists of two or more words, you shall connect them using:

1) Underscore.

2) Dash line.

3) Write the words together, without space.

4) Write the words together with a capital letter without space.

3. We can display a variable on the screen by:

1) Using the print function and passing the print command in its parentheses.

2) Using the print function and passing the value of the variable in its parentheses.

3) Using the print function and passing the name of the variable in its parentheses.

4. Arrange the parts of the code so that the program creates and displays the variable.

(name)

name

=

print

"John Doe"

CHAPTER THREE: NUMBERS

1 Integers and Fractional Numbers

You already know that integers, like 5 or 10, and fractional numbers, like 5.5 or 10.7, also called float from your school math classes. Python works with both types of numbers. It also works with relevant math operators, which are coming further.

2 Math operators

Python allows you to work with numbers using relevant math operators, that are:

1) Addition operator: +

2) Subtraction operator: -

3) Multiplication operator: *

4) Division operator: /

Let's jump into the console to work with some numbers and math operators.

3 Working with Numbers

Run empty console and hit repl button, which you already know. You should get an empty input field: input 2+2, and hit enter. What's the number you've got? Now, using the same field, subtract 5 out of 10, using subtraction operator: – Let's also multiply 5 by 5, using the multiplication operator: * Finally, let's divide 10 by 2, using the division operator: /

4 Division Without Remainder

Let's talk about division without remainder in Python. Did you notice that we've got a fraction when we divided 10 by 2, which was 5.0? But what if we need to get an integer? To get that, all we need is to use a double division operator – // Try it out. Open the input field, write 10//2, and hit enter. What number have you got now?

5 Calculation Order

Python handles calculation order, which has no difference from the order you've learned in math classes. Can you calculate (5+5)*3, then write it into the input field and check if you were right? Here is how Python will calculate it:

First, Python will calculate whatever is in brackets, following the order: first multiplication, then division, then addition, then subtraction. After that, Python will calculate whatever is around brackets, following the same order as above (multiplication, division, addition, subtraction)

Поделиться:
Популярные книги

Воронцов. Перезагрузка. Книга 3

Тарасов Ник
3. Воронцов. Перезагрузка
Фантастика:
попаданцы
альтернативная история
фэнтези
фантастика: прочее
6.00
рейтинг книги
Воронцов. Перезагрузка. Книга 3

Вампир. Английская готика. XIX век

Стивенсон Роберт Льюис
Фантастика:
ужасы и мистика
7.67
рейтинг книги
Вампир. Английская готика. XIX век

Древесный маг Орловского княжества 13

Павлов Игорь Васильевич
13. Орловское княжество
Фантастика:
аниме
фэнтези
попаданцы
5.00
рейтинг книги
Древесный маг Орловского княжества 13

Воин-Врач

Дмитриев Олег
1. Воин-Врач
Фантастика:
попаданцы
альтернативная история
историческое фэнтези
6.00
рейтинг книги
Воин-Врач

На границе империй. Том 8

INDIGO
12. Фортуна дама переменчивая
Фантастика:
космическая фантастика
попаданцы
5.00
рейтинг книги
На границе империй. Том 8

Снега

Чепурин Юлий Петрович
Поэзия:
драматургия
5.00
рейтинг книги
Снега

Прапорщик. Назад в СССР. Книга 6

Гаусс Максим
6. Второй шанс
Фантастика:
попаданцы
альтернативная история
5.00
рейтинг книги
Прапорщик. Назад в СССР. Книга 6

Кодекс Охотника. Книга XXXVII

Винокуров Юрий
37. Кодекс Охотника
Фантастика:
аниме
фэнтези
попаданцы
5.00
рейтинг книги
Кодекс Охотника. Книга XXXVII

Эпоха Опустошителя. Том V

Павлов Вел
5. Вечное Ристалище
Фантастика:
аниме
фэнтези
попаданцы
5.00
рейтинг книги
Эпоха Опустошителя. Том V

Лекарь

Первухин Андрей Евгеньевич
1. Лекарь
Фантастика:
фэнтези
попаданцы
альтернативная история
7.50
рейтинг книги
Лекарь

Ваше Сиятельство

Моури Эрли
1. Ваше Сиятельство
Фантастика:
фэнтези
попаданцы
5.00
рейтинг книги
Ваше Сиятельство

Третий Генерал: Том V

Зот Бакалавр
4. Третий Генерал
Фантастика:
городское фэнтези
аниме
сказочная фантастика
попаданцы
5.00
рейтинг книги
Третий Генерал: Том V

Цеховик. Книга 1. Отрицание

Ромов Дмитрий
1. Цеховик
Фантастика:
попаданцы
альтернативная история
5.75
рейтинг книги
Цеховик. Книга 1. Отрицание

День поминовения

Нотебоом Сэйс
Проза:
современная проза
5.00
рейтинг книги
День поминовения