In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >
Share
Shulou(Shulou.com)05/31 Report--
This article introduces you what are the practical tips of Python17, the content is very detailed, interested friends can refer to, hope to be helpful to you.
Python is the mainstream language of machine learning, not one of them. In May, it overtook JAVA in the PYPL rankings for the first time to become the world's largest programming language. A month later, Stack Overflow also shared the latest programming language pageview statistics, which showed that Python historically surpassed Java and JavaScript in the number of monthly active users and really topped the list.
Life is too short, I choose Python. So, have you really mastered Python? Now I would like to introduce 17 tips for Python beginners!
1. Swap variables
Sometimes, when we want to exchange the values of two variables, a general method is to create a temporary variable and then use it to exchange. Example:
# input
A = 5
B = 10
# create a temporary variable
Temp = a
A = b
B = temp
Print (a)
Print (b)
But in Python, we actually have a more concise way to write it:
2. The if statement is in the line
Print "Hello" if True else "World"
> Hello
3. Connect
The last way below is cool when binding two different types of objects.
Nfc = ["Packers", "49ers"]
Afc = ["Ravens", "Patriots"]
Print nfc + afc
> ['Packers',' 49ers, 'Ravens',' Patriots']
Print str (1) + "world"
> 1 world
Print `1` + "world"
> 1 world
Print 1, "world"
> 1 world
Print nfc, 1
> ['Packers',' 49ers'] 1
4. Calculation skills
# rounding down
Print 5.0//2
> > 2
# 2 to the fifth power
Print 2 release 5
> > 32
Pay attention to the division of floating point numbers
Print .3 / .1
> > 2.9999999999999996
Print .3 / / .1
> 2.0
5. Numerical comparison
X = 2
If 3 > x > 1:
Print x
> > 2
If 1
< x >0:
Print x
> > 2
6. Iterate two lists at the same time
Nfc = ["Packers", "49ers"]
Afc = ["Ravens", "Patriots"]
For teama, teamb in zip (nfc, afc):
Print teama + "vs." + teamb
> > Packers vs. Ravens
> > 49ers vs. Patriots
7. List iteration with index
Teams = ["Packers", "49ers", "Ravens", "Patriots"]
For index, team in enumerate (teams):
Print index, team
> 0 Packers
> 1 49ers
> 2 Ravens
> 3 Patriots
8. List derivation
Given a list, swipe to select the even list method:
Numbers = [1, 2, 3, 4, 5, 6]
Even = []
For number in numbers:
If number%2 = = 0:
Even.append (number)
9. Use the following instead
Numbers = [1, 2, 3, 4, 5, 6]
Even = [number for number in numbers if number%2 = = 0]
10. Dictionary derivation
Teams = ["Packers", "49ers", "Ravens", "Patriots"]
Print {key: value for value, key in enumerate (teams)}
> > {'49ersfolk: 1,' Ravens': 2, 'Patriots': 3,' Packers': 0}
11. Initialize the values of the list
Items = [0] * 3
Print items
> [0re0pl 0pl 0]
Convert the list to a string
Teams = ["Packers", "49ers", "Ravens", "Patriots"]
Print "," .join (teams)
> 'Packers, 49ers, Ravens, Patriots'
13. Get elements from a dictionary
Do not use the following ways
Data = {'user': 1,' name': 'Max',' three': 4}
Try:
Is_admin = data ['admin']
Except KeyError:
Is_admin = False
Replace with
Data = {'user': 1,' name': 'Max',' three': 4}
Is_admin = data.get ('admin', False)
14. Get the sublist
X = [1, 2, 3, 4, 5, 6]
# the first 3
Print x [: 3]
> [1pm 2pm 3]
# 4 in the middle
Print x [1:5]
>]
# the last three
Print x [- 3:]
>]
# Odd term
Print x [:: 2]
>]
# even number term
Print x [1::2]
>]
15, 60 characters to solve FizzBuzz
Some time ago, Jeff Atwood promoted a simple programming exercise called FizzBuzz. The following questions are cited:
Write a program that prints a multiple of 1 to 100 Fizz 3 to replace this number, a multiple of 5 prints "Buzz", and a number that is both a multiple of 3 and a multiple of 5 prints "FizzBuzz".
Here is a short way to solve this problem:
For x in range (101l): print "fizz" [x% 334 or:] + "buzz" [x% 514 14:] or x
16. Collection
Use the Counter library
From collections import Counter
Print Counter ("hello")
> Counter ({'luster: 2, 'hype: 1,' eBay: 1, 'oasis: 1})
17. Iterative tools
Like the collections library, there is also a library called itertools
From itertools import combinations
Teams = ["Packers", "49ers", "Ravens", "Patriots"]
For game in combinations (teams, 2):
Print game
> ('Packers',' 49ers')
> ('Packers',' Ravens')
> ('Packers',' Patriots')
> ('49ersgiving,' Ravens')
> ('49ersgiving,' Patriots')
> ('Ravens',' Patriots')
In python, True and False are global variables, so:
False = True
If False:
Print "Hello"
Else:
Print "World"
> Hello
Summary:
Python is a kind of object-oriented interpretive computer program design language. Python has rich and powerful libraries. It is often called glue language and can easily combine various modules made in other languages.
Compared with Java, C language and so on, Python is easy to learn and is more suitable for beginners without programming foundation. The Python language doesn't have much ritualization, so you can read its code even if you're not an Python expert.
The development direction of Python: data analysis, artificial intelligence, web development, testing, operation and maintenance, web security, game production and so on.
In addition, the current development trend of Python is very good. With the development of big data and artificial intelligence, the application of Python will be more popular. Python is the future of artificial intelligence. Given the flexibility of the language, its speed, and the machine learning function library provided, we will continue to see Python dominate the field of machine learning.
What are the practical tips on Python17 to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
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.