Learning python day2

--- --- restore content begins

· A variable:

Variables defined rules:

  • Variable names can be any combination of letters, numbers or underscore
  • The first character variable names can not be digital
  • The following keywords can not be declared variable name [ 'and', 'as',' assert ',' break ',' class', 'continue', 'def', 'del', 'elif', 'else', ' except ',' exec ',' finally ',' for ',' from ',' global ',' if ',' import ',' in ',' is', 'lambda', 'not', 'or' , 'pass', 'print', 'raise', 'return', 'try', 'while', 'with', 'yield']

Second basic data types:

type (): check the type

1. Digital:

  int () int

  long () Long

  float () Float

  complex () complex

 

Digital int ():

int () to convert a string of digital bits:
 

b1 = "123" # string
b2 = int (b1) # number
b2 = 123
 
###
.bit_length () of the current number is represented binary by several     
Age. 5 =
R & lt age.bit_length = ()
Print (R & lt)
###
 
 
###
num = "b"
v = int (num, Base = 16) # Base = 8, 16, means that num is a few decimal representation way, v behalf of a few decimal
print (v)
 
 
= NUM "101010"
V = int (NUM, base = 2) # hexadecimal added base is determined, the default without decimal,
Print (V)

num1="a"
v1=int(num1,base=16)
print(v1)

num2="0101010"
v2=int(num2,base=8)
print(v2)
 
= num "10"
v = int (num, Base = 16) # Base = 8, 16, means that the num-band approach is a few expressed
Print (v)
###
 
 
 


 
 
 

2. String str

eg:a3="sfgh"

.lower()/.upper()

s="fghjkkkg"

s. Lower () Returns a lowercase s # string
s . Upper () # Returns the uppercase character s

.title()/.capitalize()

s="fghjkkkg"

S . Title () # returns the string all the words the first letter capitalized and the other letters in lower case format
S . Capitalize () # returns the first letter capitalized, all other letters lowercase new string

. Swapcase () to make the case for all string conversion (uppercase -> lowercase, lowercase -> uppercase)

S . Isdecimal () determines whether the fractional
S . Isdigit ()  determines whether the digital
S . IsNumeric () whether the digital
S . Isalnum () whether the letter

S . The isalpha () letters whether

S . Islower () to determine whether lowercase
S . Isupper () to determine whether a capital
S . Istitle () to determine whether the first letter capitalized

S . Isspace becomes () determines whether the string is empty (space, tab, newline) character
S . IsPrintable () whether it is printable characters (e.g., tab, newline character is not printable, but the space is )
S . isIdentifier () satisfies the rule identifier defined

 

.center ( width , FillChar ) centering the string, using both sides FillChar filled, so that the entire string length to width. fillchar default spaces

 

 

S.ljust (width [, fillchar])    ljust()used to fill the right FillChar string S, so that the overall length to width

S.rjust (width [, fillchar])    rjust()is filled on the left. If you do not specify fillchar, the default padded with spaces

S . Zfill ( width )  filled with zeros to the left so that the length of the string S to width. If the sign S front right +/-, behind the zero-padded these two symbols, and the symbol length can be considered the

S . COUNT ( sub [, Start [, End ]]) returns the number of sub string S neutron string appear to be calculated to specify where to start (start) and calculating where to end (End), calculated from the index 0, It does not include the end border.

.find()/.rfind().index()/.rindex()  

find () contains the search string S "", if included, return "" index position, otherwise, "-1." You can specify the start and end of the end of the search start position

index () and find () the same, the only difference is that when found "" Thrown ValueErrorerror

rfind () returns the far right is the search to "" position, if only to a search or not to search for "", and then find () are equivalent, empathy rindex ()

S . Expandtabs ( N ) the string S is \treplaced with a number of spaces. Default N = 8

Whether "" the beginning or end

S.endswith(
S.startswith()

 

S.translate()

S . Partition ( On Sep )    partition(sep)dividing the first from the left On Sep
S . Rpartition ( On Sep )     rpartition(sep)dividing the first right sep.

split, rsplit and splitlines

split() According sep dividing the S

splitlines()用来专门用来分割换行符   splitlines()You can specify various line breaks, it is common \n, \r, \r\n. If you specify keepends True, then keep all the line breaks

S . The Join ( Iterable )  the iterables (Iterable) using the strings S are connected. Note, iterable must all be of type string, otherwise an error.

 

S.strip([chars]) 
S.lstrip([chars]) 
S.rstrip([chars])

It is to remove the left and right sides, respectively, on the left, the right character char. If you do not specify chars or specified as Nonethe default blank is removed (spaces, tabs, line breaks).

 

 

 

3. List list

###

1. List format: with [] enclosed
Li = [1,2,4,5, [ "SDF", "SEER", 2,3,5], "yanchuanyou", 9,7,1]
2. List in any type can be nested: letters, numbers, strings, nested anything
3 by an index value of
4 slices, the result is a list of slice
5. the support for loops, while loops
6 may be modified and deleted by indexing
7. modify and delete sections by way
8.in operation
9 with an index value
li = [1,3,4, [ "shezhengwn ", [2,3,4,5], "ales"], 5 , 6, 7 are, "Age", "yanchuanyou"]
V = Li [. 3] [. 1] [2]
Print (V)

10. String transfer list: internal loop for
S = "dfhthyuhmk"
Li = List (S)
Print (Li)
11. The table conversion string
① When the string list both numbers, write a for loop:
S = ""
Li = [1,2,4,6, "KIY", "yanchuanyou",. 4, . 6]
for I in Li:
    S = S + STR (I)
Print (S)
② only a string, using the Join
Li = [ "KIY", "yanchuanyou"]
S = "" .join (Li)
Print (S )
"" "
" ""
Li = [ "KIY", "yanchuanyou"]
S = "" .join (Li)
Print (S)
    "" "
" ""
int
STR
List
"" "
# Parameters (Li object invokes method)
# .append () is added to the last value of the last original
#li = [11,22,44,55]
# li.append (. 1)
# li.append ([ "KIY"])
# li.append ( "jjjj")
#Print (li)

# .clear () Clear List
# li.clear ()
#Print (li)
# .copy () Copy
#v = li.copy ()
#Print (V)
# .count () counts number of occurrences of element
# V = li.count (22 is)
#Print (V)
# .extend () extension, iter, iteration, iterable can be iterative, strings and lists are iterables
# each value added extend () in the end, and .append () a little different, extend to for loop
# li.extend ([2355, "dergy"])
#Print (Li)

# .index () Gets the value of the current value of the index position (left priority) according to
# V = li.index (22 is)
#Print (V)
# .insert () Insert Element specified index
# li.insert (0,99)
#Print (Li)
# .pop () to delete, delete the last element default,
# v = li.pop ()
#Print (li)
#Print (v)
delete the specified value list # .remove (), left priority
# li.remove (22)
#Print (li)

"""
删除
del
pop
remove
clear
"""
#, Reverse () reverses the list elements
# li.reverse ()
#Print (li)
# .sort () to sort
#li = [44,65,35,24,2,4,22,56]
# li.sort () # from small to large
#Print (li)
# li.sort (Reverse = True) # descending
#print (li)
 

###

 

4. tuple

# List: The list ordered list of elements can be modified to increase deleted
# li = [11,22,44,55]
# tuple: tuple ,, is the list of secondary processing
an element # tuple is not available be changed / added / deleted 
# tuple: tu = (11,2,22,33,444, )

# Can index tuple is an ordered
# TU = (11,22, [22,33,44], True, (22, 55), 89, "yhhnndh",)
# TU = V [2]
#Print (v)
Slice # can be:
# TU = (11,22, [22,33,44], True, (22, 55), 89, "yhhnndh",)
# TU = V [. 1:. 3]
#Print (V)
# Can for recycling, iterables
#for Item in TU:
# Print (Item)
# Conversion: Conversion between strings, lists, tuples
# S = "wethydgr6"
# Li = [22,3,4,66,7,4]
# TU = (33,4,56,32, "ddd" )
tuple v = # (s)
#print (v)
tuple v = # (li)
#print (v)
v = list # (s)
#print (v)
# v = list (here)
#print (v)
v str = # (li)
#print (v)
# v = p (here)
#print (v)
#tuple method
# .count () Gets the specified number of elements appearing in the tuple
# .index () Gets the specified element index tuple

 

5. Dictionary 

# Dictionary:
# key - the value of the basic structure
info = {#
# "K1": "hhh", # key (key) - the value (value) for a list of Boolean values (1 and 0 may be repeated), the dictionary can not do Key
# 2: "JJH", the #VALUE there may be numbers, strings, lists, tuples, dictionaries, Boolean
# "K3": [22 is,
# 33 is,
# 44 is,
# 55,
# [ "WWW", 00, "FFF"],
# { "KK1 ":" Vv1 ",
#" KK2 ":( 33,44,66,77)},
# (3,4,5,6,),
#" sss "]
#}
     
#Print (info)

# Dictionary are unordered
# V = info [ "K1"]
#Print (V)

# v = info [2]
#print (v)

# Can be found by indexing:
info = {
      "K1": "hhh", # key (key) - the value (value) for a list of Boolean values (1 and 0 may be repeated), the dictionary can not do Key
      2: "JJH ", #value there may be numbers, strings, lists, tuples, dictionaries, Boolean value
      " K3 ": [22 is,
            33 is,
            44 is,
            55,
            [" WWW ", 00," FFF "],
            {" KK1 ": "Vv1",
             "KK2" :( 33,44,66,77)},
             (3,4,5,6,),
             "sss"]
      }
V = info [ "K3"] [. 5] [ "KK2" ] [. 1]
Print (V)

# Dictionary can be deleted with the del
#del info [2]
#Print (info)
# Dictionary support for cycles
#for item in info: # default fetch Key
# Print (Item)
   
#for Item in info.keys (): # Gets Key
# Print (Item)
   
#for Item in info.values (): Gets # value
# Print (Item)
#for item in info.items (): #key and value while achieving
# print (item)
# Sequence to create the foundation of the dictionary, specify a uniform value
# v = dict.fromkeys ([ "11", 11, "K1"])
#Print (v)
#
# v = dict.fromkeys ([ "11", 11, "K1 "], 888)
#Print (v)
# Based on the acquired key value, when the key does not exist, can specify a default value () #
DIC = {
# "K1": "V1",
# "K2": "V2"
#}
#v DIC = [ "k11111"]
# Print (V)
# V = dic.get ( "K1", 111)
#Print (V)
# Delete: .pop () .itempop ()
() .setdefault #
# Set values: key when present, is not set, the corresponding key retrieves the current value
when the # key is not present, set, get the current value corresponding to the key
#.updata()更新
#dic={
#     "k1":"v1",
#     "k2":"v2"
#     }
#dic.update({"k1":123,
#            "k3":"yui"})
#print(dic)
#dic.update(k1=2,k3=7,k7="hjggf")
#print(dic)
 
 

III. The basic operation

+ Plus
- minus
* Multiplication
** exponentiation
/ divider 10/4 - 2.5
// 10 // List take 4-2
% 3-1% Remainder 10
== determines whether or equal to
= Assignment
> - greater than
< - less than
<= - less than or equal
> = - greater than or equal
= and <> -! is not equal to
and an AND operation
or OR operation
(the first operator parentheses brackets, from left to right without brackets operation, and and or not priority)

A Boolean value:
Ture-- true
False-- false
Members of the operation:
to determine whether something in there
in not in (the operating member)
name = "yanchuanyou"
IF "yan" name in: # consecutive characters only have the right, y, yan, right, yn wrong
    print ( "ok")
the else:
    Print ( "error")

 

 

 

--- end --- restore content

Guess you like

Origin www.cnblogs.com/yan1994/p/11488759.html