Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the grammar skills of Python

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "what are the grammar skills of Python". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn what are the grammar skills of Python.

1. For-else

What? Aren't if and else the original match? No, you may not know, else is a guy on two boats, for and else are also a couple, and it's legal. Top ten B grammar, for-else can definitely be regarded as Nanwuwan! If you don't believe me, please take a look:

> for i in: print (I) else:print (I,'I'm else') 12344 I'm else12345678910

If there is a third party if insertion between for and else (in circulation), it will not affect the relationship between for and else. Because for is at a higher level than if, and else is a powerful guy, he doesn't care if there is an if and whether he executes statements that meet the if condition. Else only sees for, and as long as for is successfully implemented, else will run again and again:

> for i in: if I > 2:print (I) else:print (I,'I am else') 344 I am else123456789

So, how to break up the enemies of for and else? The else statement is skipped only when the for loop is interrupted by the break statement:

> for i in [1, 2, 3]: if I > 2:print (I) breakelse:print (I,'I am else') 312345678

two。 One star (*) and two stars (*)

Have you found that the star (*) is really a magical symbol! Think about it, without it, what is the fun of C language? Similarly, because of it, Python will be so elegant, graceful and charming! The Python function supports default and variable parameters, with one star representing an unlimited number of single-valued parameters and two stars representing an unlimited number of key-value pairs.

Let's give an example: design a function that returns the sum of multiple input values. We can list these input values to the function, but this method is far less elegant than using the variable parameters of a star:

> def multi_sum (* args): s = 0for item in args:s + = itemreturn s > multi_sum (3meme 4pm 5) 1212345678

The Python function allows all or part of the use of fixed parameters, default parameters, single-valued (one star) variable parameters, and key-value pairs (two stars) variable parameters, which must be written in the above order.

> def do_something (name, age, gender=' male', * args, * * kwds): print ('name:% s, age:% d, gender:% s% (name, age, gender) print (args) print (kwds) > do_something (' xufive', 50, 'male', 175,75, math=99, english=90) name: xufive, age: 50, gender: male (175,75) {'math': 99,' english': 90} 123456789

In addition, one star and two stars can also be used to unpack lists, tuples, and dictionaries, which look more like C language:

> a = (1 name':'xufive', 'age':51 3) > print (a) (1 pyrrine 2 age':51 3) > print (* a) 1 23 > b = [1 name 3] > print (b) [1 reprit 2] > print (* b) 1 23 > c = {' name':'xufive', 'age':51} > print (c) {' name':'xufive', 'age':51} > > print (* c) name age > print (' name: {name}, age: {age}'. Format (* * c)) name:xufive Age:511234567891011121314151617

3. Ternary expression

Programmers familiar with python + will miss the classic ternary operator when they start using python, because it seems more troublesome to write it in python to express the same idea. For example:

> y = 5 > if y

< 0:print('y是一个负数')else:print('y是一个非负数')y是一个非负数1234567 其实,python 是支持三元表达式的,只是稍微怪异了一点,类似于我们山东人讲话。比如,山东人最喜欢用倒装句:打球去吧,要是不下雨的话;下雨,咱就去自习室。翻译成三元表达式就是: 打球去吧 if 不下雨 else 去自习室 来看看三元表达式具体的使用: >

> > y = 5 > > print ('y is a negative number'if y

< 0 else 'y是一个非负数')y是一个非负数123 python 的三元表达式也可以用来赋值: >

> > y = 5 > x =-1 if y

< 0 else 1>

> > x11234

4. With-as

The word with is not difficult to translate in English, but I can't figure out how to translate it in Python grammar. It's basically a context management protocol. As a beginner, you don't need to pay attention to the various methods and mechanisms of with, you just need to understand its application scenario. The with statement is suitable for some tasks that need to be prepared in advance and need to be handled afterwards, such as file operations, which need to open the file first and close the file after the operation is completed. If you do not use with, the file operation usually looks like this:

Fp = open (r "D:\ CSDN\ Column\ temp\ mpmap.py",'r') try: contents = fp.readlines () finally: fp.close () 12345

If you use with-as, it's much more elegant:

> with open (r "D:\ CSDN\ Column\ temp\ mpmap.py",'r') as fp:contents = fp.readlines () 12

5. List derivation

Of all the bizarre grammars, list deductions should be used the most frequently, and the simplification of the code is also obvious. For example, to find the square of each element of a list, you should usually write like this (of course, there are other ways to write it, such as using the map function):

> a = [1,2,3,4,5] > > result = list () > for i in a:result.append (iTuni) > result [1,4,9,16,25] 1234567

If you use list derivation, it looks much more comfortable:

> a = [1,2,3,4,5] > > result = [Python for i in a] > result [1,4,9,16,25] 1234 Thank you for your reading. The above is the content of "what are the grammar skills of Python?" after the study of this article, I believe you have a deeper understanding of what Python grammar skills have, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report