python full stack day5

String formatted output

name = input ( "Name:" ) # Enter your name 
age = int (input ( "Age :")) # Enter your age,
job = input ( "Job:" ) # Enter your industry
salary = input ( " salary: ") # input your income

if salary.isdigit (): # looks a lot like digital
salary = int (salary)
# the else:
# #Print (the MUST the iNPUT digit)
# Exit (" the MUST the iNPUT digit ") # exit the program

#% d only input number,% s% f placeholder is approximately equal to decimal floating-point
MSG = '' '
-------------- info of% S ----- --------
the Name:% S
Age:% D
the Job:% S
the Salary: F%
by You Will BE Retired years in S%
-------------- End --- ----------
'' '% (name, name, Age, Job, the salary, Age-60)
Print (MSG)

Data Types acquaintance 

1, Digital

2 is an example of an integer.
But long integer is an integer larger of.
3.23 and 52.3E-4 is an example of a floating point number. E denotes a power of 10 marks. Here, 52.3E-4 represents a 52.3 * 10-4.
(-5 + 4j) and (2.3-4.6j) is an example of the complex, where 5,4 is a real number, j is an imaginary number, complex mathematics represents what is? .

int (integer)

  On 32-bit machines, the integer number of bits is 32 bits, the range of -2 to 2 ** 31 ** 31-1, i.e. -2147483648 2147483647 ~
  on 64-bit system, the number of bits for 64-bit integer, in the range of -2 to 2 ** 63 ** 63-1, i.e. -9223372036854775808 ~ 9223372036854775807
long (long integer)
  with C languages, Python long integer bit width is not specified, namely: Python does not limit the size of a long integer value, but in fact because of the limited memory machines, long integer value that we use can not be infinite.
  Note that since Python2.2 on, if integer overflow occurs, Python will automatically convert into a long integer integer data, so now without the letter L in the back of a long integer data does not lead to serious consequences of.
float (float)
       Literacy http://www.cnblogs.com/alex3714/articles/5895848.html first 
  floating point real numbers for processing, i.e. with decimals. Similar to the C language type double, 8 bytes (64 bits), wherein a bottom 52, 11 represents an exponent, and the remaining one represents a symbol.
complex (complex)
  complex by a real part and an imaginary part, the general form x + yj, where x is a real part of the complex, and y is the imaginary part of the complex number, where x and y are real numbers.
Note: the presence of small cell numbers in Python: -5 to 257
 
2, Boolean
  Real or fake
  1 or 0
3, strings
"hello world"
Evil string concatenation:
  String in python reflected in the C language as an array of characters, each time you create when you need to open up a string of contiguous space in memory, and once the need to modify the string, then you need to open up space again, the evil of the + each account will appear once again to open up a space from.
 

Expressions for loop

The easiest cycle 10 times

__author__  =  'ppuy'
 
for  in  range ( 10 ):
     print ( "loop:" , i )
 
 

List (list)

      OK, now that we know the two strings and integer data types, and that demand comes, I want all the names of a class save up, how to do?

Some students said that school is not a variable stores yet, I use a variable to store chanting, Oh, not too tired you, the students, the class as a hundred people, you have to create one hundred variables ah, consumption, low efficiency .

Another student said, I do not use a large string, no problem, you really save up, however, you operate on this data (CRUD) will become very difficult, is not it, I want to know Zhang three position, how do you do?

 

In this demand, programming languages ​​have an important data type ---- List (list)

 

What is the list:

List (list) is a Python one data structure and other languages most commonly used. Python that use square brackets [] to resolve the list. The list is variable (mutable) - you can change the contents of the list.

 

A corresponding operation:

1 check ([])

1
2
3
4
5
6
7
8
9
10
names_class2 = [ '张三' , '李四' , '王五' , '赵六' ]
 
# print(names_class2[2])
# print(names_class2[0:3])
# print(names_class2[0:7])
# print(names_class2[-1])
# print(names_class2[2:3])
# print(names_class2[0:3:1])
# print(names_class2[3:0:-1])
# print(names_class2[:]) 

2 增(append,insert)

insert method for inserting objects into the list, and then append a method for adding a new end of the list of objects

1
2
3
names_class2.append( 'alex' )
names_class2.insert( 2 , 'alvin' )
print (names_class2)

3 change (reassigned)

1
2
3
4
5
names_class2 = [ '张三' , '李四' , '王五' , '赵六' ]
 
names_class2[ 3 ] = '赵七'
names_class2[ 0 : 2 ] = [ 'wusir' , 'alvin' ]
print (names_class2)

4 删(remove,del,pop)

1
2
3
4
names_class2.remove( 'alex' )
del  names_class2[ 0 ]
del  names_class2
names_class2.pop() #注意,pop是有一个返回值的 

5 Other operations

5.1  count

       statistical count the number of times an element that appears in the list:

1
2
3
4
5
6
7
>>> [ 'to' 'be' 'or' 'not' 'to' 'be' ].count( 'to'
2 
>>> x  =  [[ 1 , 2 ],  1 1 , [ 2 1 , [ 1 2 ]]] 
>>> x.count( 1
2 
>>> x.count([ 1 , 2 ]) 
1

5.2 extend

         The method can extend a plurality of values ​​at the end of another sequence in the list of additional disposable.

1
2
3
4
5
>>> a  =  [ 1 2 3
>>> b  =  [ 4 5 6
>>> a.extend(b) 
>>> a 
[ 1 2 3 4 5 6

  extend the method modifies the expanded list, the connecting operation of the original (+) is not, it will return a new list.

1
2
3
4
5
6
7
8
9
10
>>> a  =  [ 1 2 3
>>> b  =  [ 4 5 6
>>> a.extend(b) 
>>> a 
[ 1 2 3 4 5 6
>>> 
>>> a  + 
[ 1 2 3 4 5 6 4 5 6
>>> a 
[ 1 2 3 4 5 6

5.3  index

       index method is used to find the location of an index value of the first match from the list: 

1
names_class2.index( '李四' )

5.4  reverse

       reverse method in the list of storage elements in reverse.

1
2
names_class2.reverse()
print (names_class2)

5.5  sort

       The sort method for in situ to sort the list.

1
2
=  [ 4 6 2 1 7 9 ]
x.sort() #x.sort(reverse=True)

Guess you like

Origin www.cnblogs.com/ppuy/p/11520851.html