Архив рубрики: Публикации

Python: нестандартные функции. Игра “Крестики-нолики”.

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

Как создать функцию?

Общий вид функции в Python: название_функции()

Чтобы создать собственную функцию, нужно ее объявить.

Общий вид объявления функции

def название_функции(параметр1, параметр2...):

    '''Документирующая строка'''

    Блок выражений функции

Читать

Python: non-standard functions. Game “Tic-tac-toe”.

We continue to learn the Python programming language Python. Let’s move on to chapter 6 “Functions. Tic-Tac-Toe Game” from the book: Michael Dawson “Python Programming”, 2014 (Michael Dawson “Python Programming for the Absolute Beginner”, 3rd Edition) to create our own functions and work with global variables .

How to create a function?

General view of a function in Python: function_name()

To create your own function, you need to declare it.

General form of a function declaration

def function_name(parameter1, parameter2...):

    '''Document string'''

  Function expression block

Читать

Python: Lists and Dictionaries

We continue to learn the Python programming language Python. We turn to the study of chapter 5 of the book: Michael Dawson “Programming in Python”, 2014 (Michael Dawson “Python Programming for the Absolute Beginner”, 3rd Edition), where we will master lists, list methods and dictionaries.

Lists in Python

Lists are similar to tuples, which we studied in the last chapter. It’s important to remember that, unlike tuples, lists can change. The general principles for working with tuples apply to lists.

Читать

Python – логические операторы и условия

Основные условия в Python:

  • < (одно меньше другого)
  • > (одно больше другого)
  • <= (одно меньше или равно другому)
  • >= (одно больше или равно другому)
  • != (одно не равно другому)
  • == (одно равно другому)

Примеры использования условий:

print(5 > 4)

True #результат

print(‘cat’==’dog’)

False #результат Читать

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: 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,

Читать