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

Python – тернарный условный оператор (if, or, else)

Тернарный оператор (Ternary operator) – используется в строчку для задания условий в присваивании значения переменной. Легче это понять на примерах. Читать →

Python – ternary conditional statement (if, or, else)

Ternary operator (Ternary operator) – used in line to set conditions in assigning a value to a variable. It is easier to understand this with examples.

cat_say = “Mew”

me_say = “Hi,cat” if cat_say == “Mew” or cat_say == “Myavki” else “Who are you?”

print(me_say)

Hi,cat #result

Читать →

Python: типы данных и функция input()

В языке программирования Python есть несколько типов данных, например:

  • целые числа int(x), например, 1-2-3-4;
  • дробные числа float(x), например, 1.0-2.0-3.5;
  • строки str(x), в которых может быть текст и другие символы.

Функция input() уже обсуждалась ранее, эта функция дает возможность пользователю вводить данные в программу, эти данные могут дальше использоваться. Читать →

Python: циклы for, константы, срезы, кортежи, последовательности len, range, in, choice

Продолжаем учить язык программирования. Переходим к изучению 4 главы по книге: Майкл Доусон “Программируем на Python”, 2014 (Michael Dawson “Python Programming for the Absolute Beginner”, 3rd Edition), где мы изучим циклы с оператором for(), введение констант в код, срезы и кортежи, работу с последовательностями на примере строк с операторами len(), range(), in, jumble() и т.д.

Конспект 4 главы с примерами написанных мной программ:

Читать →

Python: Problems and Solutions (Chapter 6. Functions. Tic-Tac-Toe Game).

We continue to practice programming. After the sixth chapter in the book: Michael Dawson “Programming in Python”, 2014 (Michael Dawson “Python Programming for the Absolute Beginner”, 3rd Edition), where I learned how to use functions, it’s time to move on to practice. Let’s do our homework together!

Function refinement ask_number() / hod_number()

Task: Modify the ask_number() or hod_number() function so that it can be called with one more parameter – multiplicity (step size). Set the default stride to 1.

Читать →

Python: Problems and Solutions (Chapter 7 Files and Exceptions Quiz Game)

We continue to practice programming. After the seventh chapter in the book: Michael Dawson “Programming in Python”, 2014 (Michael Dawson “Python Programming for the Absolute Beginner”, 3rd Edition), where I learned how to work with files, it’s time to move on to practice. Let’s do our homework together!

A short summary of working with files in Python

Open and close a file

open() – open file and give directory path;

open("file", "r", encoding="utf-8") – open file for reading in Unicode encoding. Читать →