01_Python

Acquaintance Python

The definition of a, Python variables in

22 is = Age    # integer variable 
NAME2 = ' John Doe '   # string variables

Python type definition of variables need not take the data declaration, the type will be taken according to the judgment of the variable type variable value. The first age is an integer variable, you can get verified by type

print(type(age))

The results show that int type, we also verified the above statement.

Two, Python variable storage mechanism in memory

name = "test", the system will first character string "test" the content stored in the up and has a memory address, and then take the name of the variable that points to the "test" string, so do the variable name and the variable value between the bindings.

Extended: Python small data pool mechanism

Python automatically integer 5-256 were cached, meaning that when the value of a variable and then between this range, then Python will not take re-create an object has been created but the use of a cache object. This saves memory space. Similarly Python string will also be certain rules were cached, if a string variable in line with this rule, then Python would not go to re-create the object but to use good object has been cached.

 

Third, the variable naming guidelines:

1. hump nomenclature: NameOfStudent (beginning capital letters)

2. underline nomenclature: name_of_student

Underline nomenclature, concise recommended.

Added: constant name, constant name in all capital letters to name.

 

Fourth, comments

Notes in Python, into two single-line and multi-line comments

Comment # single number: the use of "#" You can use single-line comments (fast and built for the ctrl + /)

Multi-line comments: multi-line comments use a pair of single No. 3 '' '' '', to realize multi-line comments

Fifth, data types

5.1 numeric types Integer: int Type name = 13; float sal = 5000.6

5.2 strings of characters between single or double quotes defined to a string, character type. str = "Hello World!"

5.3 boolean, bool type value of True, False

 

Sixth, the operation of the character

6.1 remove the spaces in the string (not to remove the middle of a string space), the method name: strip (remove the spaces around the string), lstrip (remove the spaces left of the string), rstrip (remove spaces to the right of the string)

Example:

= str1 "    Hello     " 

#   removed string left and right spaces 
Print (str1.strip ()) 

#   removed string of spaces left in 
Print (str1.lstrip ()) 

#   remove the string on the right spaces 
Print (str1.rstrip ())

result

D:\Python3.6\python.exe D:/Python工具/Python_file/01/01Python.py
hello
hello    
   hello

Process finished with exit code 0

6.2 between the string case conversion, lower (all the letters in the string is converted into lowercase letters), upper (string letters all capital letters)

Case

#   String conversion between capitalization 

str2 = " AbCdEf " 

#   string in lowercase letters fully converted to 
Print (str2.lower ()) 

#   string of uppercase letters fully converted to 
Print ( str2.upper ())

result

D: \ Python3.6 \ python.exe D: / Python Tools / Python_file / 01 / 01Python.py 
abcdef cases 
ABCDEF cases 

Process finished with exit code 0

6.3 or the result determined at the beginning of the string is equal to a specified string, the method used to startswitth (determined at the beginning of the string is equal to a specified string), endsWith (the end of the string is equal to the determined specified string)

Case

#   Determines whether the beginning or end of the string is equal to the specified string 
STR = " Hello World " 

#   determines whether the beginning of the string specified string 'he' equal 
Print (str.startswith ( ' of He ' )) 

#   determines whether the end of the string to specify the string 'ld' equal 
Print (str.endswith ( ' LD ' ))

result

D:\Python3.6\python.exe D:/Python工具/Python_file/01/01Python.py
True
True

Process finished with exit code 0

6.4 join string splicing method using

To use the join between the string concatenation, and the string need to define a method used to splice a plurality of other join the string and a plurality of strings are connected to the first string is connected to the center. In particular parameters join method in the form of a list of incoming.

Case

= str1 " spacer connection string " 
str2 = " the Hello " 
Str3 = " World " 
str4 = str1.join ([str2, Str3])
 Print (str4)

result

Hello World connection string spacer

6.5 Alternative string using replace method, repalce method requires two arguments, the first is a string needs to be replaced fragment, the second parameter is the replacement string fragment. Alternatively the string needs reassigned to a string variable, because the replacement string has changed and stored in memory but not a variable name corresponding thereto.

Case

#   Alternatively string 

STR = " the Hello World " 

str2 = str.replace ( " World " , " the Python " )
 Print (str2)

result

Hello Python

Determining whether the string is a number 6.6, isdigit use, the return type is Boolean. If it is digital so will return True, if not a number will return False.

Case

#   Determines whether or not the digital string 
#   is a digital string 
str1 = " 100 " 

Print (str1.isdigit ())
 #   -numeric string 
str2 = " ABC " 

Print (str2.isdigit ())

result

True
False

Process finished with exit code 0

6.7 segmentation string, Split use, after the segmentation character string will be stored in the list.

Case

#   Cut the string points Split 
STR = ' the Hello World! ' 

Print (str.split ( "  " ))

result

['Hello', 'World!']

Process finished with exit code 0

Seven formatted output format

Sequential formatted according to the position of 7.1, both the type and the order of the placeholder character type are entered.

Case

#   Formatted output 

#   ordinal position deformatter output 
str1 = ' name is: {}, Age: {} ' 
Print (str1.format ( " Joe Smith " , " 18 " )) 

#   index order to format the output 
= str2 ' name is: {0}, Age: {}. 1 ' 
Print (str2.format ( " John Doe " , " 19 " )) 

#   according to the format output by name 
Str3 = " name of: {name} , age: Age {} " 
Print (str3.format (name = " king " , = Age "18"))

result

Name as: Joe Smith, Age: 18 
Name is: John Doe, Age: 19 
Name is: king, age: 18

 

 

Eight, classroom work

name=“aleX”

8.1 removing spaces on both sides of the value corresponding to the variable name, and outputs the processed results

#   Remove the clear space 
Print (name.strip ()) 

# output
aleX

8.2 is determined whether the value of the object variable name begins with "al", and outputs the result

#   Is determined at the beginning of the string name 
Print (name.startswith ( " Al " )) 


# Results 
False

8.3 is determined whether the variable name "X" at the end, and outputs the result

# Determines whether or not the variable name to "X" at the end, and outputs the result 
Print (name.endswith ( " X- " )) 


# results 
True

8.4 the value corresponding to the variable name in the "l" is replaced with "p", and outputs the result

# The value of the variable name corresponding to the "l" is replaced with "p", and outputs the result 
Print (name.replace ( " L " , " P " )) 


# results 
apeX

The variable name corresponding to the 8.5 value in accordance with "l" division, and outputs the result

# The name of the corresponding variable value in accordance with "l" division, and outputs the result 
Print (name.split ( " L " )) 



# Results 
[ ' A ' , ' eX ' ]

8.6 The variable name corresponding to the value programmed in uppercase, and outputs the result

# The name variable value corresponding knitting caps, and outputs the result 
Print (name.upper ()) 


# results 
 ALEX

variable name corresponding to the value 8.7 becomes lowercase, and outputs the result

# The name corresponding to the variable value becomes lower case, and outputs the result 
Print (name.lower ()) 


# results 
 alex

8.8 Please output variable name corresponding to the second character

# Please output variable name corresponding to the second character 
Print (name [. 1 ]) 


# results of 
a

8.9 Please name the output value of the variable corresponding to the first three characters

# Please output variable name corresponding to the previous value three characters 
Print (name [0:. 3 ]) 


# results 
 al

After 8.10 Please output value corresponding to the variable name character 2

# Please outputs the value of the variable name corresponding to the two characters 
Print (name [. 3:. 5 ]) 


# results 
eX

8.11 Please output index position value corresponding to the variable name in the "e" where

# Please output value corresponding to the variable name index position "e" where the 
Print (name.index ( " E " )) 



# Results 
3

8.12 obtain sequences, removing the last character

# Get sequence, remove the last character 
Print (name [0: len (name) -1 ]) 



# results 
 ale

 

Guess you like

Origin www.cnblogs.com/RenJian666/p/11079401.html