1. Number Type
2. string type
3. List type
4. The first Python Control Structure
5. References
The above two articlesArticleFamiliar with the python Development Environment: the first article mainly introduces the IDE environment for python development, which is mainly used to develop large-scale projects. The second article mainly introduces the use of the python interpreter. The following describes several common Python types: Numbers, strings, and lists.
<1>. numbers;
>>> 2 + 2 # Use the python interpreter as a calculator 4 >># this is a comment... 2 + 24> (50-5*6)/45> 7 // 3 # Floor2> 7/32> width = 20 # Assign a value to the variable >>> Height = 5*9 >>> width * height900 >>> x = y = z = 0 # Continuous assignment >>> x = y = z = 0 >>> x0 >>> y0 >>> z0 >>> undefine # The variable must be defined. Here errortraceback (most recent call last): file "<stdin>", line 1, in <module> nameerror: Name 'undefined' is not defined
>>> A = 1.5 + 0.5j # Defining the plural number
>>> A. Real # Plural real part 1.5> A. imag # Housekeeping Department 0.5> ABS () # Modulus 1.5811388300841898> _ # _ Indicates the result of the previous expression, which is 1.5811388300841898
1.5811388300841898
<2>. Strings;
>>> 'Spam eggs' # Use ''to define the string 'spam eggs '>" spam eggs" # Use "" to define the string 'spam eggs' # The following string is defined using \ in multiple rows
>>> Hello = "this is a rather long string \
... In several lines ">>> Hello 'this is a rather long string \ tin several lines' # Strings in "do not need to be transferred >>> print ("""\... usage: thingy [Options]... -H... -H hostname... ") usage: thingy [Options]-H hostname >>> word = 'help' 'A' # Concatenate strings, or use + to implement >>> word [0] # Retrieve the first character 'H'> word [0: 2] # Obtain the substring 'hes'> word [: 2] Starting from 0. # Omit the start position and obtain the child string 'hes'> word [2:] with a length of 2 from the start position 0. # Omit the end position to obtain all the characters 'lpa '> word [0] = 'X' from 2 to the end' # The error traceback (most recent call last): file "<stdin>", line 1, in <module> typeerror: 'str' object does not support item assignment >>> word [-1] #-1 indicates the last character 'a'> word [-2:] # Omit the end position to obtain the string 'pa'> S = 'supercalifragilisticexpialidocious '> Len (s) from-2) # Obtain the string length Len ()
<3>. lists;
>>> A = ['spam', 'eggs ', 100,123 4] # Define a list A. You can see that the list in python can accommodate any type >>> A ['spam', 'egg', 100,123 4] >>> Len () # List a length 4 >>> q = [2, 3] >>> P = [1, Q, 4] # Nested list type >>> Len (q) 2 >>> Len (p) 3 >>> P [1] [0] 2 >>> P [1]. append ('xtra ') # Add the element 'xtra '> P [1, [2, 3, 'xtra'], 4]> q [2, 3, 'xtra ']> A [0] = 1 # Change the first element in list A> A [1, 'eggs ', 123,123 4]
<4>. The first Python control structure;
Fibonacci. py
# Fibonacci
# The sum of the two elements defines the next
A, B = 0, 1 ;
While B < 10 :
Print (B );
A, B=B,+B;