Архив автора: admin

Python: if, elif, else branching, while loops, random number generation

We continue to learn programming. Let’s move on to the study of the third chapter of the book: Michael Dawson “Programming in Python”, 2014 (Michael Dawson “Python Programming for the Absolute Beginner”, 3rd Edition), where we will study if, elif, else conditional statements and while loops and learn how to generate random numbers using a module with the functions random.randint() and random.randrage().

Summary of chapter 3 with examples of programs I wrote:

Random Number Generation in Python

We load the import random module – it generates random numbers based on the built-in formula. To assign a random value to a variable, after calling the module, call one of its functions, for example,

Читать

Python – forbidden variable names

To find out which names cannot be given to variables in Python, use the command:

import keyword

keyword.kwlist

For Python 3.6 this words are: Читать

Python: for loops, constants, slices, tuples, sequences len, range, in, choice

We continue to learn the programming language. Let’s move on to chapter 4 of the book: Michael Dawson “Python Programming”, 2014 (Michael Dawson “Python Programming for the Absolute Beginner”, 3rd Edition), where we will study loops with the for () operator, introducing constants into code, slices and tuples, working with sequences on the example of strings with the operators len(), range(), in, jumble(), etc.

Summary of chapter 4 with examples of programs I wrote:

Cycles (loops) for

We already know loops with the while statement, and have even done a lot of tasks. Remember, there is a control variable introduced before the loop, which is then used in the condition, which in turn is tested for truth every round of the loop. So with the for loop, things are a little different. Читать

Python Error: inconsistent use of tabs and spaces in indentation

When running the code, a syntax error occurs – inconsistent use of tabs and spaces in indentation. What does this mean and how to solve this problem? The fact is that python enforces uniformity in the use of spaces, which show the indentation of parts of the code. If you use tab, it can get tricky because some of the tabs can be misinterpreted as spaces. As a result, you see a syntax error that can be difficult to fix. Читать

Python – Data Types (Objects)

In the Python programming language, there are three types objects (or data types – data type):

  • scalar (indivisible)

    • int (integers, integer, e.g. 5);
    • float (real number, numbers with a semicolon, for example: 5.5);
    • bool(true/false, true/false);

  • non-scalar (divisible)

    • str (string, string – all letters, text; everything that is not numbers).

Find out data type in Python:

type(…)

Example:

type(‘a’) – write a command

str – get the answer


Python: data types and the input() function

There are several data types in the Python programming language, for example:

  • integers int(x), for example, 1-2-3-4;
  • fractional numbers float(x), for example, 1.0-2.0-3.5;
  • str(x) strings, which can contain text and other characters.

The input() function has already been discussed earlier, this function allows the user to enter data into the program, this data can be further used.

Читать