The differences between Shell programming and Python programming

Source: Internet
Author: User
Tags for in range

Cycle

The representation of a For loop in the shell:
1, the number of paragraph form
For i in {1..10};d o echo $i;d one

2, detailed list (characters and the number of items not many)
For File in file1 file2 file3 file4 file5;do echo $File;d One

3, the existence of the file to cycle
For f in ' ls *.sh ';d o echo $f;d one


The representation of a For loop in Python:

There are two types of Python loops, one of which is the for...in loop, which sequentially iterates through each element in the list or tuple.

>>> names = ['Mic''Bob'jcy'  ] for in names: ...      Print name ... Micbobjcy
For Loop

Range (10) can generate a sequence of 0-10 integers, calculated as follows:

Sum  = 0 for in range    :+ = xprint sum
calculates the sum of 1-10 integers

The second loop is the while loop, which, as long as the condition is satisfied, loops continuously, exiting the loop when the condition is not satisfied.

For example, we want to calculate the sum of all the odd numbers within 100, which can be implemented with a while loop:

sum == while n > 0:    = sum + n    = n-2print sum
While Loop

The loop internal variable is n continuously self-reducing until -1 it becomes, and the while condition is no longer satisfied, and the loop exits.

Conditional judgment

The Python program language specifies any non-0 and non-null (NULL) values of true,0 or NULL to FALSE.

In Python programming, if statements are used in the execution of control programs, in the basic form:

if < condition judgment 1>:    < execute 1>elif < condition 2>    :< execute 2>elif < condition judgment 3>:    < execute 3>else:    < Execute 4>
If Judgment

can also be abbreviated as:

if x:     Print ' True ' as long as X is a non-0 value, a nonempty string, a non-empty list, and so on, it evaluates to true, otherwise false. 
If judgment shorthand

The differences between Shell programming and Python programming

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.