In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article focuses on "what programming skills are suitable for beginners in Python". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor learn what programming skills are suitable for beginners of Python.
Exchange variable
X = 6y = 5x, y = y, xprint x > 5print y > > 6
The if statement is in the line
Print "Hello" if True else "World" > Hello
Connect
The last way below is very co when binding two different types of objects
Nfc = ["Packers", "49ers"] afc = ["Ravens", "Patriots"] print nfc + afc > ['Packers',' 49ersgiving, 'Ravens',' Patriots'] print str (1) + "world" > 1 worldprint `1` + "world" > 1 worldprint 1, "world" > 1 worldprint nfc, 1 > ['Packers',' 49ers'] 1
Digital skills
# rounding print 5.0 down after division 2 > print 2 to the 5th power of universe 2 > > 32
Pay attention to the division of floating point numbers
Print .3 / .1 > > 2.9999999999999996print .3 / / .1 > 2.0
Numerical comparison
This is such a great simple method that I have rarely seen in many languages.
X = 2if 3 > x > 1: print x > 2if 1
< x >0: print x > 2
Iterate over 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
Indexed list iteration
Teams = ["Packers", "49ers", "Ravens", "Patriots"] for index, team in enumerate (teams): print index, team > 0 Packers > 1 49ers > > 2 Ravens > 3 Patriots
List derivation
Now that we know a list, we can select the even list method.
Numbers = [1, for number in numbers, 2, 3, 4, 5, 6] even = [] for number in numbers: if number%2 = = 0: even.append (number)
Transformed into the following:
Numbers = [1, number for number in numbers if number%2 2, 3, 4, 5, 6] even = [number for number in numbers if number%2 = = 0]
Isn't that amazing? .
Dictionary derivation
Similar to list derivation, dictionaries can do the same thing:
Teams = ["Packers", "49ers", "Ravens", "Patriots"] print {key: value for value, key in enumerate (teams)} > {'49ers: 1,' Ravens': 2, 'Patriots': 3,' Packers': 0}
Initialize the values of the list
Items = [0] * 3print items > [0je 0jue 0]
Convert list to string
Teams = ["Packers", "49ers", "Ravens", "Patriots"] print "," .join (teams) > 'Packers, 49ers, Ravens, Patriots' "
Get elements from a dictionary
I admit that the try/except code is not elegant, but here's an easy way to try to look up key in the dictionary, and if you don't find the corresponding alue, set it to its variable value with the second parameter.
Data = {'user': 1,' name': 'Max',' three': 4} try: is_admin = data ['admin'] except KeyError: is_admin = False
Replace Cheng like this:
Data = {'user': 1,' name': 'Max',' three': 4} is_admin = data.get ('admin', False)
Get a subset of the list
Sometimes, you only need some of the elements in the list. Here are some ways to get a subset of the list.
X = [1 print x [: 3] > # the first 3 print x [: 3] > [1 Magazine 2] # 4 print x in the middle [1:5] > [2 pencils 3] # the last 3 print x [- 3:] > [4 pencils 5] # Odd item print x [: 2] > [1pr 3 pas 5] # even item print x [1 print 2] >
60 characters 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, interesting way to solve this problem:
For x in range (101l): print "fizz" [x% 334 or:] + "buzz" [x% 514 14:] or x
Set
In addition to the built-in data types in python, the collection module also includes some special use cases, and Counter is very useful in some cases. If you have participated in Facebook HackerCup this year, you can even find its usefulness.
From collections import Counterprint Counter ("hello") > Counter ({'luster: 2, 'hype: 1,' eBay: 1, 'oval: 1})
Iterative tool
Like the collections library, there is another library called itertools, which is really efficient for solving certain problems. One of the use cases is to find all the combinations, which can tell you all the impossible combinations of the elements in a group.
From itertools import combinationsteams = ["Packers", "49ers", "Ravens", "Patriots"] for game in combinations (teams, 2): print game > > ('Packers',' 49ers') > > ('Packers',' Ravens') > ('Packers',' Patriots') > ('49ersgiving,' Ravens') > ('49ersgiving,' Patriots') > > ('Ravens',' Patriots')
False = = True
This is an interesting thing compared to practical techniques. In python, True and False are global variables, so
False = True if False: print "Hello" else: print "World" > > Hello
At this point, I believe that everyone on the "suitable for Python beginners programming skills have a deeper understanding, might as well come to the actual operation of it!" Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.