In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces you how to do Python script programming on Linux platform, the content is very detailed, interested friends can refer to it, I hope it can help you.
First, we'll use Python's command-line tools and touch on Python's object-oriented features (which we'll cover later in this article).
Learning Python can help you advance your career in desktop application development and data science.
Easy to use, widely used, and with a large number of "out-of-the-box" modules (a set of external files containing Python statements), Python is certainly the language of choice for programming classes taken by first-year computer science students in the United States.
In this two-article series, we'll review Python fundamentals, hoping that beginners will use this useful article as a springboard to getting started and a quick guide to using Python later.
Python in Linux
Python 2.x and 3.x are usually already built into modern Linux distributions, and you can use it right away. You can enter Python shell by typing python or python3 in terminal emulator and exit by typing quit().
$ 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 and use Python 3
One caveat, by the way: Python 2.x is not actively maintained, although it is still used. Therefore, you may want to consider switching to 3.x as indicated above. There are some differences in syntax between 2.x and 3.x, and we'll use the latter in this series.
Another way to use Python in Linux is through IDLE (the Python Integrated Development Environment), a graphical user interface for writing Python code. Before installing it, you *** check the available versions of IDLE for your Linux distribution.
# aptitude search idle [Debian and its derivatives] # yum search idle [CentOS and Fedora] # dnf search idle [Fedora 23+]
Then, you can install it as follows:
$ sudo aptitude install idle-python3.2 # I'm using Linux Mint 13
After successful installation, you will see IDLE running screen. It's a lot like a Python shell, but you can do more with it than a Python shell can do.
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 might be called "IntelliSense" or "AutoComplete" in other IDEs);
Change fonts and sizes, etc.
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 IDLE or Python shell to run the examples below, depending on your preference.
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 concatenate strings using Python.
You can also assign the result to a variable and display it on the screen. Python has a utility called concatenation-supply the print function with a comma-separated string of variables and/or characters, and it returns a sentence composed of the variables you just supplied in sequence:
>>> 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, etc.) together. When you assign a value to a variable, you can change its type later without any problems (hence Python is called a dynamically typed language).
If you try to do this in a statically typed language like Java or C#, it will throw an error.
Learn Python basics
Introduction to object-oriented programming
In object-oriented programming (OOP), all entities in a program are represented as objects, and they can interact with other objects. Therefore, objects have properties, and most objects can perform actions (this is called methods of objects).
For example, imagine creating an object called "dog." Some of the attributes it may possess are color, breed, age, etc., and the actions it can perform are call (), eat (), sleep (), and so on.
As you can see, the method name is followed by a pair of parentheses, which may contain one or more parameters (values passed into the method) or none at all.
We explain these concepts in terms of one of Python's basic object types: lists.
Explaining Object Properties and Methods: Lists in Python
A list is an ordered combination of items that do not necessarily belong to the same data type. Let's create a list called rockBands using a pair of square brackets like this:
You can add an entry to the end of the list by passing it to rockBands 'append() method, like this:
>>> rockBands = [] >>> rockBands.append("The Beatles") >>> rockBands.append("Pink Floyd") >>> rockBands.append("The Rolling Stones")
To remove an element from a 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 delete "The Beatles" from the list in this way:
>>> rockBands.remove("The Beatles")
Or in this way:
>>> rockBands.pop(0)
If you type the name of an object and then type a dot after it, you can press Ctrl + space to display a list of available methods for that object.
List available Python methods
The number of elements contained in a list is one of its attributes. It is often called "length," and you can display the length of the list by passing a list as an argument to the built-in len function (by the way, the print statement mentioned in the previous example is another Python built-in function).
If you type len in IDLE followed by an open parenthesis, you will see the default syntax for this function:
Python len function
Now let's look at 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 in the string. Do it like this:
>>> rockBands[0].upper() 'THE BEATLES' >>> len(rockBands[0]) 11
summary
In this article, we briefly introduced Python, its command-line shell, IDLE, showed how to perform arithmetic operations, how to store data in variables, how to use the print function to redisplay that data on the screen (either in itself or as part of it), and explained object properties and methods with a practical example.
About how to carry out Python script programming on the Linux platform to share here, I hope the above content can be of some help to everyone, you can learn more knowledge. If you think the article is good, you can share it so that more people can see it.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.