Encrypted page. How the HTML coding tool works. What to do to protect yourself

In which, in a condensed form,
talk about the basics of the Python language. I offer you a translation of this article. The translation is not literal. I tried to explain in more detail some points that may not be clear.

If you are planning to learn the Python language, but cannot find a suitable guide, then this
The article will be very useful to you! Behind a short time, you can meet
basics of the Python language. Although this article often relies
that you already have programming experience, but, I hope, even for beginners
this material will be useful. Read each paragraph carefully. Due to
the conciseness of the material, some topics are discussed superficially, but contain all
required metric.

Basic properties

Python does not require explicit declaration of variables, and is a case-sensitive (var variable is not equivalent to Var or VAR - they are three different variables) object-oriented language.

Syntax

Firstly, it is worth noting interesting feature Python. It does not contain operator brackets (begin..end in pascal or (..) in C), instead blocks are indented: spaces or tabs, and entering a block of statements is done with a colon. Single-line comments begin with a pound sign "#", multi-line comments begin and end with three double quotes «"""».
To assign a value to a variable, use the “=” sign, and to compare -
"==". To increase the value of a variable, or add to a string, use the “+=” operator, and “-=” to decrease it. All of these operations can interact with most types, including strings. For example


>>> myvar = 3
>>> myvar += 2
>>> myvar -= 1
""This is a multi-line comment
Lines enclosed in three double quotes are ignored"""

>>> mystring = "Hello"
>>> mystring += "world."
>>> print mystring
Hello world.
# Next line changes
the values ​​of the variables are swapped. (Just one line!)

>>> myvar, mystring = mystring, myvar

Data structures

Python contains data structures such as lists, tuples and dictionaries). Lists - similar to one-dimensional arrays (but you can use List including lists - multidimensional array), tuples are immutable lists, dictionaries are also lists, but indexes can be of any type, not just numeric. "Arrays" in Python can contain data of any type, that is, one array can contain numeric, string, and other data types. Arrays start at index 0 and the last element can be obtained at index -1 You can assign function variables and use them accordingly.


>>> sample = , ("a" , "tuple" )] #The list consists of an integer, another list and a tuple
>>> #This list contains a string, an integer and a fractional number
>>> mylist = “List item 1 again” #Change the first (zero) element of the sheet mylist
>>> mylist[-1 ] = 3 .14 #Change the last element of the sheet
>>> mydict = ("Key 1" : "Value 1" , 2 : 3 , "pi" : 3 .14 ) #Create a dictionary with numeric and integer indexes
>>> mydict["pi" ] = 3 .15 #Change the dictionary element under the index “pi”.
>>> mytuple = (1 , 2 , 3 ) #Specify a tuple
>>> myfunction = len #Python allows you to declare function synonyms this way
>>> print myfunction(list)
3

You can use part of an array by specifying the first and last index separated by a colon ":". In this case, you will receive part of the array, from the first index to the second, not inclusive. If the first element is not specified, then the count starts from the beginning of the array, and if the last element is not specified, then the array is read to the last element. Negative values ​​determine the position of the element from the end. For example:


>>> mylist = [“List item 1” , 2 , 3 .14 ]
>>> print mylist[:] #All elements of the array are read
["List item 1" , 2 , 3 .14000000000000001 ]
>>> print mylist #The zero and first element of the array are read.
["List item 1" , 2 ]
>>> print mylist[-3 :-1 ] #Elements are read from zero (-3) to second (-1) (not inclusive)
["List item 1" , 2 ]
>>> print mylist #Elements are read from first to last

Strings

Strings in Python separated by double quotes """ or single quotes """. Double quotes can contain single quotes, or vice versa. For example, the line “He said hello!” will be displayed as "He said hi!". If you need to use a string of several lines, then this line must begin and end with three double quotes """". You can substitute elements from a tuple or dictionary into the string template. The percent sign "%" between the string and the tuple replaces characters in the string “%s” to a tuple element. Dictionaries allow you to insert an element at a given index into a string. To do this, use the “%(index)s” construction in the string. In this case, instead of “%(index)s” the dictionary value at the given index will be substituted. index.


>>>print "Name: %s\nNumber: %s\nString: %s"% (my class.name, 3 , 3 * "-" )
Name: Poromenos
Number: 3
String: -
strString = """This text is located
on several lines"""

>>> print“This %(verb)s a %(noun)s.”
%("noun" : "test" , "verb" : "is")

This is a test.

Operators While statements, if for make up move operators. There is no analogue here select statement While statements, so you'll have to make do if. In the operator comparison takes place variable and list . To get a list of digits up to a number - use the range( function


). Here is an example of using operators rangelist = range(10)
>>> print#Get a list of ten numbers (from 0 to 9)
if rangelist number in rangelist:
#As long as the number variable (which increases by one each time) is included in the list...
# Check if the variable is included(3 , 4 , 7 , 9 )
While statements# numbers to a tuple of numbers number in (3 , 4 , 7 , 9 ):
#If the variable number is in the tuple (3, 4, 7, 9)... # Operation " break
» provides
# Operation "
# exit the loop at any time :
# « else continue
"scrolls"
# loop. This is not required here, since after this operation
else
# exit the loop at any time :
# « # exit the loop at any time# in any case, the program goes back to processing the loop
» It is not necessary to indicate. The condition is met # Operation "».
# if the loop was not interrupted with " pass

While statements# Nothing to do
print rangelist == 2 :
"The second item (lists are 0-based) is 2" elif
print rangelist == 3 :
# exit the loop at any time :
print"The second item (lists are 0-based) is 3"

"Dunno" while
# if the loop was not interrupted with "

rangelist == 1 :

Functions To declare a function, use keyword "» def . Function arguments are given in parentheses after the function name. You can specify optional arguments, giving them a default value. Functions can return tuples, in which case you need to write the return values ​​separated by commas. Keyword " lambda


" is used to declare elementary functions.
# unless you give them a different value when calling the function.
keyword " myfunction(arg1, arg2 = 100 , arg3 = "test" ):
return arg3, arg2, arg1
#The function is called with the value of the first argument - "Argument 1", the second - by default, and the third - "Named argument".
>>>ret1, ret2, ret3 = myfunction("Argument 1" , arg3 = "Named argument")
# ret1, ret2 and ret3 take the values ​​"Named argument", 100, "Argument 1" respectively
>>> print ret1, ret2, ret3
Named argument 100 Argument 1

# The following entry is equivalent keyword " f(x): return x+1
functionvar = . Function arguments are given in parentheses after the function name. You can specify optional arguments, giving them a default value. Functions can return tuples, in which case you need to write the return values ​​separated by commas. Keyword " x:x+1
>>> print functionvar(1)
2

Classes

The Python language is limited in multiple inheritance in classes. Internal variables and internal methods classes begin with two underscores "__" (for example "__myprivatevar"). We can also assign a value to a class variable from outside. Example:


class My class:
common = 10
keyword " __init__(self):
self .myvariable = 3
keyword " myfunction(self , arg1, arg2):
return self .myvariable

# Here we have declared the class My class. The __init__ function is called automatically when classes are initialized.
>>> classinstance = My class() # We have initialized the class and the myvariable variable has the value 3 as stated in the initialization method
>>> #Method myfunction of class My class returns the value of the variable myvariable
3
# The common variable is declared in all classes
>>> classinstance2 = My class()
>>> classesinstance.common
10
>>> classesinstance2.common
10
# So if we change its value in the My class class will change
# and its values ​​in objects initialized by the My class class
>>> Myclass.common = 30
>>> classesinstance.common
30
>>> classesinstance2.common
30
# And here we do not change the class variable. Instead of this
# we declare it in an object and assign it a new value
>>> classinstance.common = 10
>>> classesinstance.common
10
>>> classesinstance2.common
30
>>> Myclass.common = 50
# Now changing the class variable will not affect
# variable objects of this class
>>> classesinstance.common
10
>>> classesinstance2.common
50

# The next class is a descendant of the My class class
# by inheriting its properties and methods, who can the class
# inherit from several classes, in this case the entry
# like this: class Otherclass(Myclass1, Myclass2, MyclassN)
class Otherclass(Myclass):
keyword " __init__(self, arg1):
self .myvariable = 3
print arg1

>>> classinstance = Otherclass("hello")
hello
>>> classesinstance.myfunction(1 , 2 )
3
# This class does not have the property test, but we can
# declare such a variable for an object. Moreover
# this variable will only be a member class instance.
>>> classinstance.test = 10
>>> classesinstance.test
10

Exceptions

Exceptions in Python have a structure try-except [except ionname]:


keyword " somefunction():
try :
# Division by zero causes an error
10 / 0
except ZeroDivisionError:
# But the program does not "Perform an illegal operation"
# And handles the exception block corresponding to the “ZeroDivisionError” error
print"Oops, invalid."

>>>fn except()
Oops, invalid.

Import

External libraries can be connected using the procedure “ import", where is the name of the library being connected. You can also use the command " from import" so you can use a function from the library


import random #Import the “random” library
from time import clock #And at the same time the “clock” function from the “time” library

Randomint = random .randint(1 , 100 )
>>> print randomint
64

Working with the file system

Python has many built-in libraries. In this example we will try to save in binary file list structure, read it and store the line in text file. To transform the data structure we will use the standard library "pickle"


import pickle
mylist = ["This" , "is" , 4 , 13327 ]
# Open the file C:\binary.dat for writing. "r" symbol
# prevents replacement of special characters (such as \n, \t, \b, etc.).
myfile = file (r"C:\binary.dat" , "w")
pickle .dump(mylist, myfile)
myfile.close()

Myfile = file (r"C:\text.txt" , "w")
myfile.write("This is a sample string")
myfile.close()

Myfile = file (r"C:\text.txt")
>>> print myfile.read()
"This is a sample string"
myfile.close()

# Open the file for reading
myfile = file (r"C:\binary.dat")
loadedlist = pickle .load(myfile)
myfile.close()
>>> print loadedlist
["This" , "is" , 4 , 13327 ]

Peculiarities

  • Conditions can be combined. 1 < a < 3 выполняется тогда, когда а больше 1, но меньше 3.
  • Use the operation " del" to clear variables or array elements.
  • Python offers great opportunities for working with lists. You can use list structure declaration operators. Operator if allows you to specify list elements in a certain sequence, A While statements- allows you to select elements by condition.
>>> lst1 =
>>> lst2 =
>>> print
>>> print
# The "any" operator returns true if although
# if one of the conditions included in it is satisfied.
>>> any(i % 3 if i in )
True
# The following procedure counts the number
# matching elements in the list
>>> sum (1 if i in While statements i == 3)
3
>>> del lst1
>>> print lst1
>>> del lst1
  • Global Variables are declared outside functions and can be read without any declarations. But if you need to change the value of a global variable from a function, then you need to declare it at the beginning of the function keyword « global" if you don't do this, then Python will declare a variable that is only accessible to that function.
number = 5

keyword " myfunc():
# Outputs 5
print number

keyword " anotherfunc():
# This throws an exception because the global variable
# was not called from a function. Python in this case creates
# variable of the same name inside this function and accessible
# only for operators of this function.
print number
number = 3

keyword " yetanotherfunc():
global number
# And only from this function the value of the variable is changed.
number = 3

Epilogue

Of course, this article does not describe all the features of Python. I hope this article will help you if you want to continue learning this programming language.

Benefits of Python

  • The execution speed of programs written in Python is very high. This is due to the fact that the main Python libraries
    are written in C++ and take less time to complete tasks than other high-level languages.
  • Because of this, you can write your own Python modules in C or C++
  • In the standard Python libraries you can find tools for working with by email, protocols
    Internet, FTP, HTTP, databases, etc.
  • Scripts written with Python help run on most modern operating systems. This portability ensures Python application in a wide variety of areas.
  • Python is suitable for any programming solution, be it office programs, web applications, GUI applications, etc.
  • Thousands of enthusiasts from all over the world worked on the development of Python. Support modern technologies in the standard libraries we can owe it precisely to the fact that Python was open to everyone.

Tags: Add tags

The program is a set of algorithms that ensure that the necessary actions are performed. Conventionally, in the same way you can program ordinary person, writing the exact commands so that, for example, he prepares tea. If the latter option will use natural speech (Russian, Ukrainian, English, Korean, etc.), then the computer will need special language programming. Python is one of them. The programming environment will subsequently translate the commands into and the human goal for which the algorithm was created will be fulfilled. Python has its own syntax, which will be discussed below.

History of the language

Development began in the 1980s and ended in 1991. The Python language was created by Guido van Rossum. Although the main symbol of Python is a snake, it was named after the American comedy show.

When creating the language, the developer used some commands borrowed from existing Pascal, C and C++. After going online first official version a whole group of programmers joined in its refinement and improvement.

One of the factors that allowed Python to become quite famous is its design. He is recognized by many highly successful specialists as one of the best.

Features of Python

The Python programming language will be an excellent teacher for beginners. It has a fairly simple syntax. It will be easy to understand the code, because it does not include many auxiliary elements, and the special structure of the language will teach you how to indent. Of course, a well-designed program with a small number of commands will be immediately understandable.

Many syntactic systems were created using object-oriented programming. Python is no exception. Why exactly was he born? It will make it easier for beginners to learn and will help already qualified employees remember some elements.

Language syntax

As already mentioned, the code is quite easy and simple to read. Python has sequential commands that are precise in execution. In principle, the operators used will not seem difficult even to beginners. This is what makes Python different. Its syntax is easy and simple.

Traditional operators:

  • When specifying conditions you should use if-else construct. If there are too many such lines, you can enter the elif command.
  • Class is for understanding the class.
  • One of simple operators- pass. It does nothing, fits for empty blocks.
  • Cyclic commands are while and for.
  • The function, method and generator are defined thanks to def.

In addition to single words, the Python programming language allows you to use expressions as operators. By using string chains, you can reduce the number of separate commands and parentheses. So-called lazy calculations are also used, i.e. those that are performed only when the condition requires it. These include and and or.

Program writing process

The interpreter works on a single mechanism: when you write a line (after which you put “Enter”), it is immediately executed, and a person can already see some result. This will be useful and quite convenient for beginners or those who want to test a small piece of code. In compiled environments, you would have to first write the entire program, only then run it and check for errors.

Python programming language (for beginners, as has already become clear, it is ideal) in operating system Linux allows you to work directly in the console itself. Should be written to command line Python code name English language. It won't be difficult to create your first program. First of all, it is worth considering that the interpreter can be used here as a calculator. Since young and novice specialists are often not comfortable with syntax, you can write the algorithm this way:

After each line you must put “Enter”. The answer will be displayed immediately after you click it.

Data used by Python

The data that computers (and programming languages) use comes in several types, and this is quite obvious. Numbers can be fractional, integer, can consist of many digits, or can be quite massive due to the fractional part. To make it easier for the interpreter to work with them, and for him to understand what he is dealing with, a specific type should be specified. Moreover, it is necessary for the numbers to fit into the allocated memory cell.

The most common data types used by the Python programming language are:

  • Integer. It's about about integers that have both negative and positive values. Zero is also included in this type.
  • In order for the interpreter to understand that it is working with in fractional parts, should be set float type point. As a rule, it is used when using numbers with a varying point. It should be remembered that when writing a program, you need to stick to the notation “3.25” and not use the comma “3.25”.
  • In the case of adding strings, the Python programming language allows you to add a string type. Often words or phrases are enclosed in single or

Disadvantages and advantages

In the last few decades, people have been more interested in spending more time mastering data and less time getting it processed by computers. Language about which there are only positive things is the highest code.

Python has virtually no disadvantages. The only serious disadvantage is the slowness of the algorithm execution. Yes, if you compare it with “C” or “Java”, it is, frankly speaking, a turtle. This is explained by the fact that this

The developer made sure to add the best things to Python. Therefore, when using it, you can notice that it has absorbed best features others higher languages programming.

In the event that the idea that is implemented by the interpreter is not impressive, then it will be possible to understand this almost immediately, after writing several dozen lines. If the program is worthwhile, then the critical section can be improved at any time.

Currently, more than one group of programmers is working on improving Python, so it is not a fact that code written in C++ will be better than that created using Python.

Which version is better to work with?

Nowadays, two versions of such a syntactic system as the Python language are widely used. For beginners, choosing between them will be quite difficult. It should be noted that 3.x is still in development (although released to the masses), while 2.x is a fully completed version. Many people advise using 2.7.8, as it practically does not lag or crash. There are no radical changes in version 3.x, so you can transfer your code to the programming environment with an update at any time. To download the necessary program, you should go to the official website, select your operating system and wait until the download completes.

Python is a popular and powerful scripting language with which you can do anything you want. For example, you can crawl websites and collect data from them, create networking and tools, perform calculations, program for Raspberry Pi, develop graphics programs and even video games. In Python you can \\ write system programs, platform independent.

In this article we will look at the basics of programming in Python, we will try to cover all the basic features that you will need to start using the language. We will look at using classes and methods to solve various tasks. It is assumed that you are already familiar with the basics and syntax of the language.

What is Python?

I will not go into the history of the creation and development of the language, you can easily find out from the video that will be attached below. It's important to note that Python is scripting language. This means your code is checked for errors and immediately executed without any additional compilation or rework. This approach is also called interpretable.

This reduces productivity, but is very convenient. There is an interpreter in which you can enter commands and immediately see their results. Such interactive work Helps a lot in learning.

Working in the interpreter

Running the Python interpreter is very easy on any operating system. For example, on Linux, just type the python command in the terminal:

In the interpreter prompt that opens, we see the version of Python that is currently in use. Nowadays, two versions of Python 2 and Python 3 are very common. They are both popular because many programs and libraries were developed on the first, and the second has more features. Therefore, distributions include both versions. By default, the second version is launched. But if you need version 3, then you need to do:

It is the third version that will be considered in this article. Now let's look at the main features of this language.

String Operations

Strings in Python are immutable; you cannot change one of the characters in a string. Any change to the content requires creating a new copy. Open the interpreter and follow the examples listed below in order to better understand everything written:

1. Concatenate strings

str = "welcome " + "to python"
print (str)

2. String multiplication

str = "Losst" * 2
print (str)

3. Merging with transformation

You can concatenate a string with a number or a boolean value. But for this you need to use a transformation. There is a str() function for this:

str = "This is a test number " + str(15)
print (str)

4. Search for a substring

You can find a character or substring using the find method:

str = "Welcome to the site"
print(str.find("site"))

This method displays the position of the first occurrence of the substring site if it is found; if nothing is found, then the value -1 is returned. The function starts the search at the first character, but you can start at the nth character, for example 26:

str = "Welcome to the site site"
print(str.find("losst",26))

In this variant, the function will return -1 because the string was not found.

5. Getting a substring

We got the position of the substring we are looking for, and now how to get the substring itself and what comes after it? To do this, use this syntax [start:end],just specify two numbers or just the first one:

str = "One two three"
print(str[:2])
print(str)
print(str)
print(str[-1])

The first line will print a substring from the first to the second character, the second - from the second to the end. Please note that the countdown starts from zero. To count in reverse order, use a negative number.

6. Substring replacement

You can replace part of a string using the replace method:

str = "This site is about Linux"
str2 = str.replace("Linux", "Windows")
print(str2)

If there are many occurrences, then you can replace only the first one:

str = "This is a site about Linux and I am subscribed to this site"
str2 = str.replace("site", "page",1)
print(str2)

7. Cleaning up strings

You can delete extra spaces using the strip function:

str = "This is a website about Linux"
print(str.strip())

You can also remove extra spaces only on the right side with rstrip or only on the left side with lstrip.

8. Changing the register

There are special functions to change the case of characters:

str="Welcome to Lost"
print(str.upper())
print(str.lower())

9. Converting strings

There are several functions to convert a string to different numeric types, these are int(), float(), long() and others. The int() function converts to an integer and float() to a floating point number:

str="10"
str2="20"
print(str+str2)
print(int(str)+int(str2))

10. Length of lines

You can use min(), max(), len() functions to calculate the number of characters in a line:

str="Welcome to the Losst website"
print(min(str))
print(max(str))
print(len(str))

The first one shows minimum size character, the second is the maximum, and the third is the total length of the string.

11. Iterate over a string

You can access each character of a string individually using a for loop:

str="Welcome to the site"
for i in range(len(str)):
print(str[i])

To limit the loop, we used the len() function. Pay attention to the indentation. Python programming is based on this, there are no parentheses to organize blocks, just indentation.

Operations with numbers

Numbers in Python are fairly easy to declare or use in methods. You can create integers or floating point numbers:

num1 = 15
num2 = 3.14

1. Rounding numbers

You can round a number using the round function, just specify how many digits you want to leave:

a=15.5652645
print(round(a,2))

2. Random number generation

Get random numbers you can use the random module:

import random
print(random.random())

By default, the number is generated from the range 0.0 to 1.0. But you can set your own range:

import random
numbers=
print(random.choice(numbers))

Operations with date and time

The Python programming language has a DateTime module that allows you to various operations with date and time:

import datetime
cur_date = datetime.datetime.now()
print(cur_date)
print(cur_date.year)
print(cur_date.day)
print(cur_date.weekday())
print(cur_date.month)
print(cur_date.time())

The example shows how to extract the desired value from an object. You can get the difference between two objects:

import datetime
time1 = datetime.datetime.now()
time2 = datetime.datetime.now()
timediff = time2 - time1
print(timediff.microseconds)

You can create date objects yourself with an arbitrary value:

time1 = datetime.datetime.now()
time2 = datetime.timedelta(days=3)
time3=time1+time2
print(time3.date())

1. Formatting date and time

The strftime method allows you to change the date and time format depending on the selected standard or specified format. Here are the basic formatting characters:

  • %a- day of the week, abbreviated name;
  • %A- day of the week, full name;
  • %w- number of the day of the week, from 0 to 6;
  • %d- day of the month;
  • %b- abbreviated name of the month;
  • %B- full name of the month;
  • %m- month number;
  • %Y- year number;
  • %H- hour of the day in 24 hour format;
  • %l- hour of the day in 12 hour format;
  • %p- AM or PM;
  • %M- minute;
  • %S- second.

import datetime
date1 = datetime.datetime.now()
print(date1.strftime("%d. %B %Y %I:%M%p"))

2. Create a date from a string

You can use the strptime() function to create a date object from a string:

import datetime
date1=datetime.datetime.strptime("2016-11-21", "%Y-%m-%d")
date2=datetime.datetime(year=2015, month=11, day=21)
print(date1);
print(date2);

File system operations

File management is very easy in Python programming language, it is best language for working with files. And in general, we can say that Python is the simplest language.

1. Copying files

To copy files you need to use the functions from the subutil module:

import shutil
new_path = shutil.copy("file1.txt", "file2.txt")

new_path = shutil.copy("file1.txt", "file2.txt", follow_symlinks=False)

2. Moving files

Moving files is done using the move function:

shutil.move("file1.txt", "file3.txt")

The rename function from the os module allows you to rename files:

import os
os.rename("file1.txt", "file3.txt")

3. Reading and writing text files

You can use built-in functions to open files, read or write data to them:

fd = open("file1.txt")
content = fd.read()
print(content)

First you need to open the file to work using the open function. To read data from a file, the read function is used, the read text will be saved into a variable. You can specify the number of bytes to be read:

fd = open("file1.txt")
content = fd.read(20)
print(content)

If the file is too large, you can split it into lines and process it like this:

content = fd.readlines()
print(content)

To write data to a file, it must first be opened for writing. There are two modes of operation - overwriting and adding to the end of the file. Recording Mode:

fd = open("file1.txt","w")

And adding to the end of the file:

fd = open("file1.txt","a")
content = fd.write("New content")

4. Creating directories

To create a directory use the mkdir function from the os module:

import os
os.mkdir("./new folder")

5. Getting creation time

You can use getmtime(), getatime() and getctime() functions to get the time last change, last accessed and created. The result will be displayed in Unix format, so it needs to be converted to readable form:

import os
import datetime
tim=os.path.getctime("./file1.txt")
print(datetime.datetime.fromtimestamp(tim))

6. File list

With the listdir() function you can get a list of files in a folder:

import os
files= os.listdir(".")
print(files)

To solve the same problem, you can use the glob module:

import globe
files=glob.glob("*")
print(files)

7. Serializing Python Objects

import pickle
fd = open("myfile.pk ", "wb")
pickle.dump(mydata,fd)

Then to restore the object use:

import pickle
fd = open("myfile.pk ", "rb")
mydata = pickle.load(fd)

8. File compression

The Python standard library allows you to work with various formats archives, for example zip, tar, gzip, bzip2. To view the contents of a file use:

import zipfile
my_zip = zipfile.ZipFile("my_file.zip", mode="r")
print(file.namelist())

And for creating zip archive:

import zipfile
file=zipfile.ZipFile("files.zip","w")
file.write("file1.txt")
file.close()

You can also unpack the archive:

import zipfile
file=zipfile.ZipFile("files.zip","r")
file.extractall()
file.close()

You can add files to the archive like this:

import zipfile
file=zipfile.ZipFile("files.zip","a")
file.write("file2.txt")
file.close()

9. Parsing CSV and Excel files

Using the pandas module, you can view and parse the contents of CSV and Excel tables. First you need to install the module using pip:

sudo pip install pandas

Then to parse, type:

import pandas
data=pandas.read_csv("file.csv)

By default, pandas uses the first column for the headings of each row. You can specify a column for the index using the index_col parameter, or specify False if it is not needed. To write changes to a file, use the to_csv function:

data.to_csv("file.csv)

You can parse the Excel file in the same way:

data = pd.read_excel("file.xls", sheetname="Sheet1")

If you need to open all tables, use:

data = pd.ExcelFile("file.xls")

Then you can write all the data back:

data.to_excel("file.xls", sheet="Sheet1")

Networking in Python

Python 3 programming often involves networking. The Python standard library includes socket capabilities for low-level network access. This is necessary to support multiple network protocols.

import socket
host = "192.168.1.5"
port = 4040
my_sock = socket.create_connection((host, port))

This code connects to port 4040 on machine 192.168.1.5. When the socket is open, you can send and receive data:

my_sock.sendall(b"Hello World")

We need to write the character b before the line because we need to transfer data in binary mode. If the message is too big, you can iterate:

msg = b"Longer Message Goes Here"
mesglen = len(msg)
total = 0
while total< msglen:
sent = my_sock.send(msg)
total = total + sent

To receive data, you also need to open a socket, but use the my_sock_recv method:

data_in = my_sock.recv(2000)

Here we indicate how much data needs to be received - 20000, the data will not be transferred to the variable until 20000 bytes of data have been received. If the message is larger, then to receive it you need to create a loop:

buffer = bytearray(b" " * 2000)
my_sock.recv_into(buffer)

If the buffer is empty, the received message will be written there.

Working with mail

The Python standard library allows you to receive and send email messages.

1. Receiving mail from a POP3 server

To receive messages we use a POP server:

import getpass,poplib
pop_serv = poplib.POP3("192.168.1.5")
pop_serv.user("myuser")
pop_serv.pass_(getpass.getpass())

The getpass module allows you to obtain the user's password in a secure manner so that it is not displayed on the screen. If the POP server uses a secure connection, you need to use the POP3_SSL class. If the connection is successful, you can interact with the server:

msg_list = pop_serv.list() # to list the messages
msg_count = pop_serv.msg_count()

To finish the job use:

2. Receiving mail from the IMAP server

To connect and work with the IMAP server, use the imaplib module:

import imaplib, getpass
my_imap = imaplib.IMAP4("imap.server.com")
my_imap.login("myuser", getpass.getpass())

If your IMAP server uses a secure connection, you need to use the IMAP4_SSL class. To get a list of messages use:

data = my_imap.search(None, "ALL")

You can then loop through the selected list and read each message:

msg = my_imap.fetch(email_id, "(RFC822)")

But, don't forget to close the connection:

my_imap.close()
my_imap.logout()

3. Sending mail

Used to send mail SMTP protocol and the smtplib module:

import smtplib, getpass
my_smtp = smtplib.SMTP(smtp.server.com")
my_smtp.login("myuser", getpass.getpass())

As before, use SMTP_SSL for a secure connection. When the connection is established, you can send a message:

from_addr = " [email protected]"
to_addr = " [email protected]"
msg = "From: [email protected]\r\nTo: [email protected]\r\n\r\nHello, this is a test message"
my_smtp.sendmail(from_addr, to_addr, msg)

Working with web pages

Python programming is often used to write various scripts for working with the web.

1. Web crawling

The urllib module allows you to query web pages different ways. To send a regular request, use the request class. For example, let's perform a normal page request:

import urllib.request
my_web = urllib.request.urlopen("https://www.google.com")
print(my_web.read())

2. Using the POST method

If you need to submit a web form, you must use GET request, and POST:

import urllib.request
mydata = b"Your Data Goes Here"
my_req = urllib.request.Request("http://localhost", data=mydata,method="POST")
my_form = urllib.request.urlopen(my_req)
print(my_form.status)

3. Create a web server

Using the Socket class, you can accept incoming connections, which means you can create a web server with minimal capabilities:

import socket
host = ""
port = 4242
my_server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
my_server.bind((host, port))
my_server.listen(1)

When the server is created. you can start accepting connections:

addr = my_server.accept()
print("Connected from host ", addr)
data = conn.recv(1024)

And don't forget to close the connection:

Multithreading

Like most modern languages Python allows you to run multiple parallel threads, which can be useful if you need to perform complex calculations. IN standard library There is a threading module that contains the Therad class:

import threading
def print_message():
print("The message got printed from a different thread")
my_thread = threading.Thread(target=print_message)
my_thread.start()

If the function is running for too long, you can check if everything is ok using the is_alive() function. Sometimes your threads need to access global resources. Locks are used for this:

import threading
num = 1
my_lock = threading.Lock()
def my_func():
global num, my_lock
my_lock.acquire()
sum = num + 1
print(sum)
my_lock.release()
my_thread = threading.Thread(target=my_func)
my_thread.start()

conclusions

In this article we covered the basics python programming. Now you know most of the frequently used functions and can use them in your small programs. You will love programming in Python 3, it's very easy! If you have any questions, ask in the comments!

To end the article, there is an excellent lecture on Python: