Архив метки: Python

Python – использование format в строках

Внутри строки мы можем указать как фиксированный текст, так и часть текста, определяемого функцией format, который будет состоять из отдельно задаваемых участков:

print(“This cat has {0} kittens and a {1}”.format(2,”tail”))

Вместо использования номеров внутри фигурных скобок можно в них сразу задать значение переменной – a,b,c:

print(‘quantity: ({a}, {b}, {c})’.format(a=5,b=2,c=4)) Читать

Python: лайфхаки по настройке среды программирования

Как только вы установили на компьютер Python, лучше сразу позаботиться о небольших нюансах, которые облегают жизнь программистам. Давайте пройдемся по основным настройкам среды программирования и Windows для комфортной работы:

1. Изменение системных переменных среды

Эта настройка позволит запускать программу, написанную на Python, в CMD операционной системы Windows (интерпретатор командной строки). Чтобы это сделать, нажимаем “Пуск” -> Изменение системных переменных среды: Читать

Python: lifehacks for setting up a programming environment

Once you’ve installed Python on your computer, it’s best to take care of the little nuances that make life easier for programmers. Let’s go through the basic settings of the programming environment and Windows for comfortable work:

1. Changing System Environment Variables

This setting will allow you to run a program written in Python in the CMD of the Windows operating system (command line interpreter). To do this, click “Start” -> Change system environment variables:

Читать

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 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: 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. Читать