day01 pyhon basis

Class Notes

A programming language

What is a programming language?
Expression can be recognized by a computer programming language mentioned above, i.e., language communication medium, and a computer programming language to communicate with a programmer medium. In the programming world, a computer is more like a slave to the purpose of human programmed to command the slaves to work.

What is programming?
Programming that is based on the needs of programmers to write down their thoughts flow in accordance with the syntax of a programming language style, the result is the output file that contains a bunch of characters.

Stressed:
the program is no different with the ordinary file before is not running, the characters only at run time, the paper wrote only specific grammatical meaning.

The second computer part

  • CPU
  • RAM
  • External memory
  • input device
  • Output Device

Installation and use of three of python

1. Install the python interpreter
2. Install pycharm editor
3. Write python code, and outputs the print hello world!

Four variables (amount of change)

Variable value: true to store a memory address in memory.
Variable name: variable values for binding relationship.
= Number assignment: to bind the value of the variable to the variable name.

1, the variable naming conventions

Hump ​​nomenclature: AgeOfMe

Underline name: age_of_me (recommended)

2, variable names defined specification

  • To start with an English letter or an underscore, such as: al, _a
  • Numbers can not start (1a)
  • Use keywords can not be named

 

3, the variable name specification

Underline name: age_of_me = 18

Bad way: the Chinese name, the variable name is too long, do not convey a variable term

4, three characteristics defined variables

Copy the code
# Id (used to represent the value of a variable in a unique memory address in memory) essentially the same as the value of the variable, the memory address is not the same 
  NAME1 = 'Tank1' 
  NAME2 = 'Tank1' where # is the same due to the there optimization mechanism 
## python optimization mechanism 
## (small cell count: in a certain length, python the same value of the variable value in the consolidated stored in the same memory address)   Print (the above mentioned id (NAME1)) # of the type (variable values type)   str1 = 'Hello'   Print (type (str1)) # variable determines the type of type # value (variable value)   str2 = 'Hello'   Print (str1 == str2)
Copy the code

 Five constant (invariable amount)

  • Named to all uppercase. eg SCHOOL = 'Hefei' .etc
  • Not can not modify them, but everyone who ordained all uppercase variables are called constants, can not be modified.

Six user to interact with the program

  • Input: input () // python3 any data type string is entered, python2 need to indicate the type of
  • Output: print ()
# Allow users to input 
name = the INPUT ( 'Please the Enter your name:') 
Print (name) 
# comment 
-line comment: # (shortcut: Ctrl + /) 
multi-line comments: '' '' '' (Shortcut: + Enter key)

 Seven formatted output

eg Dear customer, hello! Your month deducted 99 yuan bill, left 0.

Placeholder:
% S: can replace any type of
% d: can be replaced by a digital type

Example:
Dear customer, hello! Your phone bill this month deducted yuan% s,% d remaining yuan.

Copy the code
Alternatively to # 100 to the S% 
str1 = 'Dear User, Hello! Your phone bill this month deducted from% s yuan, left 0. '100% 

# The alternative to a one hundred% S, up to 50 to replace the D% 
str1 = "Dear Customer, Hello! Your phone bill this month deducted yuan% s,% d remaining yuan. '% (One Million', 50) 
Print (str1) 

# error 
str1 = 'Dear customer, hello! Your phone bill this month deducted yuan% s,% d remaining yuan. '% (One Million', '50') 
Print (str1)
Copy the code

 Eight basic data types

1. Digital Automatic Identification :( type)

  • Integer: int
  • Float: float

2. String type

  • Role: name, sex, nationality, address and other descriptive information
  • Definition: single quotation marks \ double quotes \ three marks, a string of characters name = 'tank'
    # 1 single quotes 
     2 str1 = 'king cover tiger' 
     . 3 Print (str1) 
     . 4 Print (type (str1)) 
     . 5 double quotes # 
     6 str2 = "Mickey Mouse met" 
     . 7 Print (str2) 
     . 8 Print (type (str2 )) 
     9 # three marks 
    10 Str3 = '' ' 
    . 11 Anhui 
    12 Hefei 
    13 newbest INSTITUTE' '' 
    14 Print (Str3) 
    15 Print (type (Str3))

Master priority actions:

1, according to the index value (Forward + Reverse take take) 

# Forward taken 
str1 = 'Tank Hello!' 
Print (str1 [0]) # H 
Print (str1 [. 9]) # K 
# take reverse 
print (str1 [-2]) # k

2, a slice (care regardless of the end, step)

Copy the code
# Care regardless tail 
str1 = 'Tank Hello!' 
Print (str1 [0:. 4]) 
Print (str1 [0:. 5]) 
# step 
! Print (str1 [0:11]) # Hello Tank 
Print (str1 [ 0: 11: 2]) # hlotn!
Copy the code

3, the length len

print (len (str1)) 
# 11

4, members and not in operation in

print('h'in str1)#true
print('h'not in str1)#false

5, remove the blank strip 

# Will remove whitespace string left and right sides of the 
print (str1.spirt ())

 6, slicing split 

print(name.split('x'))

 7, circulation

You need to know:

1、strip,lstrip,rstrip

str1=' hello kugou '
print(str1.strip())
print(str1.lstrip())
print(str1.rstrip())

2、lower,upper

= str1 'Hey Guys' 
Print (str1.lower ()) # converted to lower 
print (str1.upper ()) # Uppercase

3、startswith,endswith

= str1 'Hey Guys' 
Print (str1.startswith ( 'Hey')) is determined at the beginning character is # # True Hey 
Print (str1.endswith ( 'Guys')) Similarly #True #

4, format three games are played

Copy the code
= str1 'My name IS% S, S My Age%'% ( 'Tank', 18 is) 
Print (str1) 
# formatted in accordance with an order of position way 
print ( 'my name is {} , my age {}!'. the format ( 'Tank', 18 is)) 
# format index according to embodiment two 
Print ( 'My name IS {0}, Age {My}. 1!'. the format ( 'Tank', 18 is)) 
# third approach by name format of 
print ( 'my name is {name }, my age {age}!'. format (age = 18, name = 'tank')) # can change the position of
Copy the code

5、split,rsplit
6、join

# ## allows only given string concatenation 
#Print ( '' .join ([ 'Tank', 18 is])) ## according to a space, to list each string splicing print ( '' .join ([ ' Tank ',' 18 is', 'from GZ'])) ## according to _, the list for each string splicing print ( '_'. join ( [ 'tank', '18', 'from GZ' ]))

7、replace

str1='my name is LiLei,my age 200!'
print(str1)
str2=str1.replace('LiLei','damn')
print(str2)

8, isdigit 

choice=input('please choose [0,1,2]:')
print(choice.isdigit())

 

 


operation

   name=' aleX'

   1) corresponding to the variable name is removed on both sides of the space values, and outputs the processing result

    2) determination whether the value corresponding to the variable name beginning with "al", and outputs the result


    3) corresponding to the variable name is determined whether the value of an "X" at the end, and outputs the result


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

    5) The name of the corresponding variable value in accordance with "l" division, and outputs the result.

    6) The name becomes a variable value corresponding to upper case, and outputs the result


    7) The name corresponding to the variable value becomes lower case, and the output


    8) Please outputs of the two character values ​​corresponding to the variable name?

    9) Please output the first three characters of the name value of the corresponding variable?

    10) Please post the output value of two variables corresponding character name?


    11) Request output value corresponding to the variable name index where "e" position?


    12) acquisition sequences, removing the last character. Such as: oldboy then get oldbo.

 

Copy the code
= name 'Alex' 
Print ( 'removable clear space', name.strip ()) 
Print ( "whether" al "beginning", name.startswith ( 'Al')) 
Print ( "whether the" X "at the end ' , name.endswith ( 'X-')) 
Print ( 'the "l" is replaced with "P"', name.replace ( '. 1', 'P')) 
Print ( 'the "l" split', name.split ( 'L')) 
Print ( 'uppercase', name.upper ()) 
Print ( 'lowercase', name.lower ()) 
Print ( 'output of the second character', name [2]) 
Print ( 'first three characters', name [:. 3]) 
Print (' two character ', name [-2:]) 
Print (' E location ', name.index (' E ')) 
Print (' remove the last character ', name [: - 1] )
Copy the code

 

Guess you like

Origin www.cnblogs.com/rui-1234/p/11080413.html