Python's 10 common code shorthand technique, one is the entry to read, understand all the great god!

AI era, python programming language outlet stand off, in July 2018 the world list of programming languages ​​ranked in the first three programming languages, IEEE's top interactive programming language Python's 2018 rankings Tu list, thorough fire python, have also let more people into the program in the army.

So the question is, without any programming foundation, and English is not good, how to learn python programming language? hey, guys, do not be afraid, small series to help you.

Today I will give you put together a 10 programmers code commonly used shorthand technique, one is the entry read, all understand that the great God, you know a few do?

1. ternary operator

When the write if ... else statement, instead of using the ternary operator.

const x = 20;let answer;if (x > 10) {

Shorthand:

const answer = x > 10 ? 'is greater' : 'is lesser';

Can also be nested if statements:

const big = x > 10 ? " greater 10" : x

Do not understand the learning process can join my python zero-based systems Learning Exchange Qiuqiu qun: 784758,214, current sharing Python enterprise talent needs and how Python from the zero-based learning with you, and learn what content. Related video learning materials, development tools have to share

2. The short-circuit evaluation shorthand

When another value assigned to a variable, like primordial determination value is not null, undefined or a null value. You can write to write a multi condition of the if statement.

Or you may use short-circuit evaluation method:

const variable2 = variable1 || 'new';

3. Declare variables shorthand method

let x;let y;let z = 3;

Shorthand method:

let x, y, z=3;

Conditions exist shorthand method 4.if

if (likeJavaScript === true)

Shorthand:

if (likeJavaScript)

only

likeJavaScript

When the value is true, both statements are equal

If the value is not a true value is determined, it can be:

let a;if ( a !== true ) {// do something...}

Shorthand:

let a;if ( !a ) {// do something...}

5.JavaScript cycle shorthand method

for (let i = 0; i < allImgs.length; i++)

Shorthand:

for (let index in allImgs)

You can also use Array.forEach:

6. short-circuit evaluation

To the value of a variable is assigned by determining whether the value is null or undefined, it can be:

let dbHost;if (process.env.DB_HOST) {

Shorthand:

const dbHost = process.env.DB_HOST || 'localhost';

7. Decimal index

When it is desired to write a lot of numbers with zero (e.g. 10000000), may be employed index (1E7) instead of this figure:

for (let i = 0; i < 10000; i++) {}

Shorthand:

8. Object Properties shorthand

If the attribute name and the name of the same key, the method may be employed for ES6:

const obj = { x:x, y:y };

Shorthand:

const obj = { x, y };

9. Function abbreviated arrow

The traditional method is very easy to write a function to understand and write, but when nested within another function, these advantages will be gone.

function sayHello(name) { console.log('Hello', name);

Shorthand:

sayHello = name => console.log('Hello', name);

10. The implicit return value shorthand

Is often used to return the return statement function final result, a single function arrow implicit statement can return its value (the function must be omitted

{}为了省略return关键字)为返回多行语句(例如对象字面表达式),则需要使用()包围函数体。

学习过程中有不懂的可以加入我的python零基础系统学习交流秋秋qun:784758,214,与你分享Python企业当下人才需求及怎么从零基础学习Python,和学习什么内容。相关学习视频资料、开发工具都有分享

简写:

Guess you like

Origin blog.csdn.net/kkk123789/article/details/92007257