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

Python: Challenges and Decisions (Chapter 2. Types, Variables, and Basic I/O. Useless Facts Program).

We continue to learn programming. After the second chapter in the book: Michael Dawson “We Program in Python”, 2014 (Michael Dawson “Python Programming for the Absolute Beginner”, 3rd Edition), where I studied the features of working with text in the Python programming language, tasks were offered. Let’s do them together. I will give my solution, and you write your options in the comments.

1) Come up with two lists: valid and invalid variable names. Explain why each of the names is valid or not valid, respectively. Then come up with two more lists—of “good” and “bad” valid names—and explain your choice.

Valid variable names: name, price, password, age, size (these can be any words that are not reserved in the programming language for functions / operators)

Читать

Python: Challenges and Solutions (Chapter 1. Getting Started. The Game Over Program).

We continue to learn programming. After the second chapter in the book: Michael Dawson “We Program in Python”, 2014 (Michael Dawson “Python Programming for the Absolute Beginner”, 3rd Edition), where I studied the features of working with text in the Python programming language, tasks were offered. Let’s do them together. I will give my solution, and you write your options in the comments.

1) Learn how to trigger a bug: Interactively enter the name of your favorite ice cream brand. Then fix the error and create a command that will correctly display the name on the screen this type of ice cream.

Читать

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.

Читать

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: Basic Text Input and Output Operators

The very first text operations and basic information about Python that helps you start programming right away:

# Comments

Delimiters ” or “” and escaping characters ’ and ”

print(‘Hello world’) – newline

end=’…’ – next print will be glued

escape sequences:

a – system speaker

n – newline (empty line)

t – tab type indent

+ string concatenation without delimiter

* repeat lines without delimiter

continuation of a line of code on the next line

input(‘Press Enter to exit’)

Basic mathematical operators (+,-,*,/,//,%).

Compound assignment statements

String methods (applied to a string with text): I am cat

.upper() I Am CAT

.lower() i am cat

.title() I Am Cat

.replace(old,new,max number of replacements) replaces the old text with the new one

.swapcase() reverse case i Am Cat

.capitalize() the first letter is capital, the rest are lowercase I am cat

.strip() string without intervals Iamcat

Читать

Python: базовые операторы ввода и вывода текста

Самые первые операции с текстом и базовые сведения о Python, которые помогают тут же начать программировать:

  1. # Comments
  2. Ограничители '' или "" и экранирование символов ' и "
  3. print('Hello world') – переход на новую строку
  4. end='...' – следующий print склеится
  5. escape-последовательности:

    a – системный динамик

    n – переход на новую строку (пустая строка)

    t – отступ типа tab
  6. + сцепление строк без разделителя
  7. * повторение строк без разделителя
  8. продолжение строки кода на следующей строке
  9. input('Press Entr to exit')
  10. Базовые математические операторы (+,-,*,/,//,%).
  11. Составные операторы присвоения
  12. Строковые методы (применяются к строке с текстом): I am cat

    .upper() I Am CAT

    .lower() i am cat

    .title() I Am Cat

    .replace(old,new,max число замен) заменяет старый текст на новый

    .swapcase() меняет регистры наоборот i Am Cat

    .capitalize() первая буква большая, остальные строчные I am cat

    .strip() строка без интервалов Iamcat

Читать