Linux Tool-Getting Started with Python scripting (i)

Source: Internet
Author: User
Tags arithmetic contact form linux mint

Guide As we all know, system administrators need to be proficient in a scripting language, and so will the job requirements listed by the recruiting agencies. Most people think that Bash (or some other shell language) is easy to use, but some powerful languages (like Python) can bring you some other benefits.

First, we'll use Python's command-line tools, and we'll touch Python's object-oriented features (the second half of this article talks about it).

Learning Python can help you develop your career in desktop application development and data science.

Easy to use, widely used, with a massive "out-of-the-box" module (which is a set of external files containing Python statements), Python is rightly the choice of the language used by American computer majors in their first-year programming classes.

In this two-story series, we'll review the basics of Python, and hopefully you'll be able to use this practical article as a springboard for getting started in programming, and a quick guide to using Python later. the Python in Linux

Python 2.x and 3.x are typically built into a modern Linux distribution, and you can use it immediately. You can enter "Python" or "Python3" in the terminal simulator to enter the Python shell and enter "quit ()" to exit.

$ which Python
$ which python3
$ python-v
$ python3-v
$ python
>>> quit ()
$ python3
  >>> quit ()

Running Python commands in Linux

If you want to use Python 3.x instead of 2.x when typing "python", you can change the corresponding symbolic link as follows:

$ sudo rm/usr/bin/python 
$ cd/usr/bin
$ ln-s python3.2 python # Choose the Python 3.x binary here

Remove Python 2, use Python 3

Incidentally, one thing to note: Although Python 2.x is still in use, it is not actively maintained. Therefore, you may want to consider switching to 3.x as indicated above. The syntax for 2.x and 3.x is somewhat different and we will use the latter in this series of articles.

Another way to use Python in Linux is through the IDLE (Python integrated development environment), a graphical user interface for writing Python code. Before you install it, it's a good idea to look at the available versions of IDLE that apply to your Linux distribution.

# Aptitude Search Idle     [Debian and its derived distributions]
# Yum Search idle          [CentOS and Fedora]
# DNF Search Idle          [fedora 23+ Version]

Then, you can install it as follows:

$ sudo aptitude install idle-python3.2    # I ' m using Linux Mint 13

After the installation is successful, you will see the IDLE run screen. It's like a python shell, but you can use it to do more than the Python shell does.

For example, you can: easily open external files (file→open);

Python Shell Copy (Ctrl + C) and paste (Ctrl + V) text, find and replace text, display possible code completion (a feature that may be called IntelliSense or AutoComplete in other ides), change font and font size, and so on.

Most of all, you can use IDLE to create desktop apps.

We don't develop desktop apps in either of these articles, so you can choose IDLE or Python shells to run the following example, depending on your preferences. basic operations in Python

As you might expect, you can do arithmetic operations directly (you could use enough parentheses in all your operations). , you can also easily use Python stitching strings.

You can also assign the result of the operation to a variable and then display it on the screen. Python has a utility called stitching (concatenation)--gives the print function a string of variables and/or strings separated by commas, which returns a sentence consisting of the variables you have just provided:

>>> A = 5
>>> b = 8
>>> x = b/a
>>> x
1.6
>>> print (b, "Divided by", A, "equals", X)

Note that you can mix different types of variables (numbers, strings, Boolean symbols, and so on) together. When you assign a value to a variable, you can then change its type without any problems (so Python is called the dynamic type language).

If you try to do this in a static type language (such as Java or C #), it throws an error.

a brief introduction to learning Python's basic operations object-oriented programming

In object-oriented programming (OOP), all entities in a program are rendered in the form of objects, and they can interact with other objects. Therefore, objects have attributes, and most objects can perform actions (this is called the object's method).

For example: Let's imagine creating an object "dog". Some of the attributes it might have are color, variety, age, and so on, and it can be done with the action called (), eating (), sleeping (), and so on.

As you can see, the method name follows a pair of parentheses, which may contain one or more parameters (the values passed to the method), and may not contain anything.

We explain these concepts in one of the basic object types of Python--a list. To interpret the properties and methods of an object: A list in Python

Lists are an ordered combination of entries, and the data types that these entries belong to do not need to be the same. We use a pair of brackets to create a list called "Rockbands" like the following:

You can pass an entry to the "Append ()" Method of "rockbands" to add it to the end of the list, as follows:

>>> rockbands = []
>>> rockbands.append ("The Beatles")
>>> rockbands.append ("Pink Floyd ")
>>> rockbands.append (" The Rolling Stones ")

To remove an element from the list, we can pass a specific element to the remove () method, or the position of the element to be removed from the list in pop () (counting from 0).

In other words, we can remove the "Beatles" from the list in the following way:

>>> Rockbands.remove ("The Beatles")

Or in this way:

>>> Rockbands.pop (0)

If you enter the name of the object and then enter a point at the back, you can press Ctrl + space to display the list of available methods for the object.

Lists the available Python methods

The number of elements contained in the list is one of its properties. It is often called "length," and you can display the length of the list by passing a list to the built-in function "Len" (the print statement mentioned in the previous example, incidentally, is another built-in function of Python).

If you enter "Len" in the IDLE and then follow an unclosed parenthesis, you will see the default syntax for this function:

The Len function of Python

Now let's take a look at the specific entries in the list. Do they also have attributes and methods? The answer is yes. For example, you can convert a string entry to uppercase and get the number of characters the string contains. Do as follows:

>>> rockbands[0].upper ()
' The BEATLES '
>>> len (rockbands[0])
11
Summary

In this article, we briefly describe Python, its command-line shell, IDLE, how to perform arithmetic operations, how to store data in a variable, how to use the "print" function to display the data on the screen again (whether they are part of them), The properties and methods of the object are also explained by a practical example.

In the next article, we'll show you how to use conditional statements and loop statements to implement process control. We will also explain how to write a script to help us complete our system management tasks.

Do you want to continue to learn some knowledge about Python? Please look forward to the second part of this series (we'll combine the advantages of Python and command-line tools in our script) and you can also consider buying our ultimate Python programming tutorial.

As usual, if you have any questions about this article, you can ask us for help. You can use the following contact form to send us a message, we will reply you as soon as possible.




This article is reproduced from: http://www.linuxprobe.com/linux-python-start1.html

Free to provide the latest Linux technology tutorials Books, for the open source technology enthusiasts to do more and better: http://www.linuxprobe.com/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.