Linux Tool-Getting Started with Python scripting (i)

Source: Internet
Author: User
Tags contact form linux mint

Guide As is known to all, system administrators need to be proficient in a scripting language, and the job requirements listed by the recruiting organization will also be written. Most people think that Bash (or other shell language) is handy, but some powerful languages (like Python) can give you some other benefits.

First, we'll use Python's command-line tools, and we'll be exposed to Python's object-oriented features (the latter part of this article talks to 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 that contain Python statements), Python has rightly become the perfect choice for the language used by American computer majors in programming classes at first grade.

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

Python in Linux

Python 2.x and 3.x are often built into modern Linux distributions, and you can use it immediately. You can enter "Python" or "Python3" in the terminal emulator 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

Delete python 2, using Python 3

Incidentally, one thing to note: Although Python 2.x is still being used, 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'll 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 installing it, you'd better check out the IDLE version available for your Linux distribution.

# Aptitude Search Idle     [Debian and its derivative 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 much like a python shell, but you can do more with it than the Python shell does.

For example, you can:

    1. Easily open external files (file→open);

      Python Shell
    2. Copy (Ctrl + C) and paste (Ctrl + V) text;
    3. Find and replace text;
    4. Show possible code completion (a feature that may be called "IntelliSense" or "auto-complete" in other Ides);
    5. Change the font and size, and so on.

Best of all, you can create desktop apps with IDLE.

We won't be developing desktop apps in these two articles, so you can choose the IDLE or Python Shell to run the following example, depending on your preferences.

basic operations in Python

As you might expect, you can perform arithmetic operations directly (you can use enough parentheses in all your operations!) ), you can also easily use Python to stitch 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), which provides the print function with a comma-delimited set of variables and/or strings, which returns a sentence consisting of the variables you just provided:

>>> a = 5>>> b = 8>>> x = b/a>>> x1.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 statically typed language (such as Java or C #), it throws an error.

Learn the basic operations of Python a brief introduction to object-oriented programming

In object-oriented programming (OOP), all entities in a program are rendered as objects, and they can interact with other objects. Therefore, objects have properties, 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 properties it may have are color, variety, age, and so on, while the actions it can perform are called (), eaten (), slept (), and so forth.

As you can see, the method name is followed by a pair of parentheses, which may contain one or more parameters (the value passed to the method), or it may not contain anything.

We use one of the basic object types of Python--a list to explain these concepts.

explaining the properties and methods of an object: list in Python

Lists are an ordered combination of entries that do not need to have the same data type. Let's use a pair of parentheses to create a list called "Rockbands", like this:

You can pass an entry to the "Append ()" Method of "rockbands" to add it to the tail 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 pass the position of the element to be deleted in the list (counting from 0) to pop ().

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 later, you can press Ctrl + space to display a list of available methods for that object.

List the available Python methods

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

If you enter "Len" in 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 properties and methods? The answer is yes. For example, you can convert a string entry to uppercase and get the number of characters that the string contains. Do this as follows:

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

In this article, we briefly describe Python, its command-line shell, IDLE, shows how to perform arithmetic operations, how to store data in variables, how to use the "print" function to re-display the data on the screen (either themselves or part of it). 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 with our system administration tasks.

Do you want to continue to learn some knowledge about Python? Look forward to the second part of this series (we'll combine the benefits of Python and command-line tools in the script), and you can also consider buying our Ultimate Python Programming series tutorial.

As always, if you have any questions about this article, you can ask us for help. You can use the contact form below to send us a message and we will get back to you as soon as possible.

Linux Tool-Getting Started with Python scripting (i)

Related Article

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.