The basic components of python

First, the constant

Which represents the amount of constant change, in fact, the amount of real change does not exist in python, the unspoken rules only when we want to define a variable named constant is the constant capital:

For example: a person's age, growing, then the older called constants, if the person has died, his age was fixed in the old year, and this time the age is constant

AGE=60

Second, the user program (python) interaction

First of all what is interactive, interactive dialogue between people just like you have to words, I have to go to language, can talk to ... be able to communicate, how to dialogue with the user program it? At this time we can call a python's built-in functions, called: input

Examples of
name = input () print (name ) # If you want the program to have a message
name = the INPUT ( "the What's your name?")
Print (name)

Third, comments

As more and more code, if you do not write comments if possible even a few days to write their own code to write do not know what it meant, let alone others, so you need to have notes to prompt the user for
one-line comments can be used # said right annotation content number #
if you want to use multi-line comments can '' '' '', while the left and right sides of a middle part between quotes 3 is annotated content

Fourth, data types

digital

int Integer
defined: age = 10 # age = int (10)
for identifying: age, grade, ID number, qq number, floating-point number #float
defined: salary = 3.1 # salary = float (3.1)
with to identify: wages, height, weight,

 String

And the difference between the role of single quotes, and more quotes, the three quotes # multi-line strings need to use triple quotes
double and single quotes are used to define the line string, then what difference does it make two persons, for example
want how this sentence is defined as a string do?

what's your name
this sentence which has a single quotation mark, and if we use single quotes wrapped him up, he'll get an error so we need outside wrapped in double quotes
res = "what's your name"

In addition python which can be digital, the string can do? Let me tell you out loud, can
a = 'you' b = 'good' a + b # result is 'hello'

Note that, only strings and string concatenation, and other data types can not be summed

List

Within [] are separated by commas, can be stored in any number of any type of value, such as (numbers, strings, lists, tuples are OK), # values ​​for identifying a plurality of storage, such as a plurality of students in the class, a people have multiple interests

>>> test = [1,2,'a',[1,2],{'a','b'}] 
>>> test
[1, 2, 'a', [1, 2], {'b', 'a'}] 
>>>

利用下标取列表里的值
>>> test[0]
1
>>>

Tuple

Tuples can access the same list with multiple values, but most are used to read a tuple of

dictionary

Now that you have access to a list of multiple values, why should we have dictionaries? For example you will understand
such as this there are two values in a list of 28 to indicate age, height used to identify a 187, but there is no explanation, that element corresponds to the age, that element corresponds to the height

info = [28,187] #因此,字典就可以解决这个问题
info = {'age':28,'high':187}
info={ 'name':'fengzi', 'hobbies':['play','sleep'], 
'company_info':{ 'name':'宏福', 'type':'education', }
info['company_info']['name']#取公司名字

Boolean

Boolean is True and False

>>> a=100
>>> b=200
>>> 
>>> a > b #不成立就是False,也就是假
False 
>>> a < b #成立就是True, 也就是真
True #谨记 #0,None,空都为假,其余为真

Six, and a variable type immutable type

1. Variable Type: id in the case of the same, value can be changed, is called a variable type, such as lists, dictionaries

  1. Immutable type: value once changed, id also changed, it is called immutable type (id change, means creating a new memory space)

Seven formatted output

Often, there is a scene in the program: require user input, and then printed in a fixed format such as requiring the user to enter the user # name and age, and then print the following format:
. My name IS XXX, XXX # My Age IS this case to use to% s and% d
RES = 'My name IS% s, My Age IS% d'% ( 'crazy', 29) print (res) # noted here% d only receive number,% s can receive digital be You may receive a character string

The first method, transmission parameters
res = 'my name is {name }, my age is {age}'. Format (name = ' mad', Age = 29)
Print (RES)

The second method sequentially
res = 'my name is {0 }, my age is {1}'. Format ( ' crazy', 29)
Print (RES)

Eight basic operators

a = 10     b=20

image

 Nine, comparison operators

image

 X. assignment operator

image

 Eleven, the identity of the operator

is represented id are equal, which is relatively memory address is identical, the True, the inconsistency is False

== indicating whether the two values ​​are identical, the True, the inconsistency is False

Guess you like

Origin www.cnblogs.com/LibetJohn/p/11126599.html