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
Let’s analyze each paragraph of this summary in more detail, so that it is easier to understand what’s what:
Contents:
1. # Comments
It is customary to start the program with comments that indicate the name, date of creation of the code, author, and other notes. If you write the symbol # at the beginning of the line and insert any text after it, then the computer will not consider this text as part of the program. These comments are made not for the computer, but for people, so that programmers in the future can quickly figure out what’s what.
2. Delimiters ” or ” “
In Python, everything inside a statement, for example,
print (“written inside delimiters”).
Such a program will already give us the result:
written inside delimiters
Delimiters show where the line starts and where it ends. Where to start executing the code, and where to end. We put quotes at the beginning and at the end, it can be either single (apostrophes) or double quotes. If the text itself inside the delimiters also contains quotes, then we escape them with a backslash \” or \’ – in this case, the computer understands that this is not the end of the line, but just text with quotes.”
So if we want to write: is written inside the “delimiters”, the program will look like this:
print (“written inside \”delimiters\””).
If you want to set an empty string as the value of a variable, you can write:
peremennaya = ' '
If you want to set an empty value for a variable, you can write:
peremennaya = None
3. The print('Hello world')
statement prints the text and puts us on a new line.
Everything that will be written further we will see the line below.
4. Line end operator
If we want to continue writing something after the first print without going to the next line, then we will use the end-of-line operator end=” means that ‘inside the delimiters we will indicate what the next print should be “pasted” to. Let our end be a space:
print (“written inside”, end=” “)
print(“delimiters”)
Such a program will already give us the already familiar result:
written inside delimiters
If you remove end:
print (“written inside”)
print(“delimiters”)
We get the result:
written inside
limiters
5. \
escape-sequences
— it is
- or a backslash before the special character (see point 1, where we escaped quotes \’ or \”);
- or is it a backslash followed by a letter:
\a
– calling system speaker sound;
\n
– newline (empty line);
\t
– indent with tab.
6. Plus (+) when working with text.
“Plus” works with text as a concatenation without a separator.
If the text parts are added inside the brackets, then a space must be added to one of them, for example, after the word “written by_”:
print (“written ” + “inside delimiters”)
Such a program will already give the result:
written inside delimiters
7. Multiplication sign (*) when working with text.
“asterisk” works with text as a repetition of lines without a delimiter:
print (“written “*4)
The program will give the result:
is being written is being written is being written is being written
8. Splitting a string \
For the convenience of human perception of the code, we may want to split a long string into several parts located on different lines, so that the computer considers it all to be one line, put a backslash character (\) at the split point, and simply go to the next line and continue writing there the code. The entire string will be combined by the computer into one expression.
9. input function
The input function makes it possible to receive text from the keyboard, that is, we can, through the expression input(‘Press Entr to exit’) – show the user the text Press Entr to exit and wait until he presses Entr, and then the program will end. It is a way of interactive interaction with the person who sees our program. In principle, we can accept the text that the user writes and write it as a new variable, with which we can somehow operate further. But for starters, you can leave everything like that, and then after pressing Entr, the output of the input function will end.
An example of the “Introduction” program:
name=input(‘What is your name?\n’)
print(‘Hi,’,name)
10. Basic Math Operators
Addition, subtraction, multiplication, and other operators in Python work on both numbers and text (see string concatenation and repetition).
Basic operators and their meaning:
+ addition
– subtraction
* multiplication
/ division
// integer division (with remainder). Example: 10//6=1
% remainder after integer division. Example: 10%6=4
11. Compound assignment statements
… allow you to assign a value to a variable based on the original value.
For example, you had the value a=5, and you want to make the same variable equal to 5+4. Basically, you can just write:
a=5 (source variable)
a=5+4 (new variable value)
There is a short way to do this:
a=5
a+=4
So += is a compound assignment operator. -=, *=, /=, %= and others work in a similar way.
12. String Methods
…allow you to change lines with a single command:
Program example:
quote=‘I am cat’
print(quote)
print(quote.upper())
print(quote.lower())
print(quote.capitalize())
print(quote.title())
print(quote.strip())
print(quote.swapcase())
print(quote.replace(‘cat’,‘bear’))
Book summary: Майкл Доусон “Программируем на Python”, 2014 (Michael Dawson “Python Programming for the Absolute Beginner”, 3rd Edition).