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

Python – строки, конкатенация и повторение строк

Чтобы присвоить переменной строку, нужно заключить тест либо в апострофы ‘…’, либо в двойные кавычки “…”:

string=’I am a cat’

string=”I am a cat”

Если в строке есть апостроф (I’m), то всю строку нужно заключить в двойные кавычки, и наоборот. Если в строке есть и апостроф и двойные кавычки, то нужно использовать обратный слэш , чтобы экранировать символы. Читать

Python – структуры ветвления (операторы if else, if, ifelif)

Оператор if (если) используется для задания условий (если это так, то…), например:

cat_say = ‘mew’

if cat_say = ‘mew’ of cat_say = ‘myavki’:

….print(‘Dear, cat! Here’s your food!’)

Важно: используя условия if, нужно писать двойное равно и в конце выражения ставить двоеточие, чтобы показать конец условия; перед остальной частью кода, относящейся к условию, и print – отступ через tab (или 4 пробела). Читать

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

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

cat_say = “Mew”

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

print(me_say)

Hi,cat #результат

Тернарный оператор по сути включает в себя имя переменной, которой мы присваиваем значение и внутри этой же строки задаем условия, описываемые операторами if/or/else. Не обязательно использовать все три if/or/else оператора, в условии может использоваться просто if/else.

Важно: тернарный условный оператор в Python может использоваться как со строками, так и с числами.

Все уроки по Python


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

Python – programming environment and additional programs

When you have installed Python, the question arises – how to start learning this language? Should I write commands through the console or do I need to install something else on the computer? There are many programming environments and solutions for Python, but for a beginner, it is preferable to choose from two:

  • if you are learning Python for scientific purposes, then download Anaconda from www.continuum.io/downloads – this environment already includes Python, as well as such useful programs for programming and analytics as Spyder, Jupyter, IPython, R and others.
  • you are learning Python for general development, then install the cool Sublime Text code editor from sublimetext.com; in this case, you will need to manually configure the Python interpreter to run the program written in this editor.

Читать