python and the input and output list and using the tuple

python input and output

python output
used print()in brackets input string, you can specify the output of our text.

print('你是真的秀')

The following are the results of our operation:
Here Insert Picture Description
use print()can also accept multiple strings, use a comma "," separated, can be connected into a string output

print('你是真的秀', 'A1高闪来一个', 'wdnmd')

The following are the results of our operation:
Here Insert Picture Description

print()Print each string in turn, met comma "," will output a space.

print()It may be an integer or print the results:

print(300)
print(400+500)
 print('400 + 500 = ',400 + 500)

Our operating results are as follows:
Here Insert Picture Description
Input
If you enter some characters from your computer how to do? We can use input()the input string and place them into a variable.

name = input()

Here Insert Picture Description
To print out the namecontents of a variable, you can directly write nameand then press ENTER outside, you can also use print()the function

name
print(name)

The following are the results of our operation:
Here Insert Picture Description
when the program is running, we can also write some tips

name = input('please enter your name: ')
print('hello,', name)

The following are the results of our operation:
Here Insert Picture Description

list

Python built-in data type is a list: list. list is an ordered set, which you can add and remove elements at any time.

classmates = ['Michael', 'Bob', 'Tracy']

Variable classmates is a list. The number with len () function can be obtained list of elements:

len(classmates)

The following are the results of our operation:
Here Insert Picture Description
you can use the index to access the list of elements in each position, remember that the index is zero-based:

classmates[0]
classmates[1]
classmates[2]

The following are the results of our operation:
Here Insert Picture Description
If you want to take the last element, in addition to calculate the index position, you can also do the index by -1, direct access to the last element:

classmates[-1]

The following are the results of running:
Here Insert Picture Description
list ordered list is a variable, it can be added to the list of elements to the end:

classmates.append('Adam')

Here is our operating results:
Here Insert Picture Description
can also be elements inserted into the specified location, such as an index number 1 position:

classmates.insert(1'aaa')

Here are the results:
Here Insert Picture Description

tuple

Another called an ordered list of tuples: tuple. tuple and the list is very similar, but the tuple can not be changed once initialized:

classmates = ('Michael', 'Bob', 'Tracy')

Here Insert Picture Description
Now, classmates this tuple can not be changed, nor does it append (), this method insert (). Other methods of obtaining and list element are the same, you can use normal classmates [0], classmates [-1 ], but can not be assigned to another element.
Immutable tuple What is the significance? Because tuple immutable, so the code more secure. If possible, use a tuple instead of a list as much as possible tuple.
When you define a tuple, when defined, tuple elements must be finalized.

More detailed and specific tutorial available here:
https://www.liaoxuefeng.com/wiki/1016959663602400/1017092876846880

Guess you like

Origin blog.csdn.net/fight252/article/details/90608284