Python Primer One

Source: Internet
Author: User

01 Data types

1. What is a data type
The value of the variable is the data we store, so the data class refers to the different kinds of variable values

2, why the data to be divided into types?
Variable values are used to preserve the state of the real world, so different types of data should be used to represent different states.


3, how to use, that is, the classification of data types?

‘‘‘
One: Number type
integer int
1, Function: The age of the person, various numbers, grade

2. Definition
Age=18 #age =int (18)

Print (ID (age))
Print (Type (age))
Print (age)

3. How to use


Float Type Floats
1, the role: to indicate height, weight, salary ...

2. Definition
salary=3.1 #salary =float (3.1)
Print (ID (salary))
Print (Type (salary))
Print (Salary)

3. How to use


C. String Type str
1. Function: Describe a state of nature, such as a person's name, home address

2. Definition: A string of characters contained within single quotation marks, double quotes, and three quotation marks # name= ' Egon ' #name =str (' Egon ')

Msg= ""
Alex
Egon
Wxx
‘‘‘
Print (Type (name))
Print (Type (msg))

Note the point:
Msg= "My name is ' Egon '"
Print (msg)

3. How to use
String types can only: + or *
msg= ' Hello '
Name= ' Egon '

Print (msg + name)

Name= ' Egon '
Print (name * 10)

Print (1)
Print (2)
Print (3)
Print ('-' *100)
Print (1)
Print (2)
Print (3)


msg1= ' Zaello '
msg2= ' za '


Print (Msg2 > MSG1)

Print (' A ' > ' Z ')


Four: List of lists
1, function: Used to access and put multiple values

2, how to define: in [] separated by a comma to open multiple values of any type
L=[1, ' A ', 3.1,[1,3]] #l =list ([1, ' A ', 3.1,[1,3]])
Print (ID (l))
Print (Type (l))
Print (L)
x=111
L=[1,2,x, ' a ']
Print (L)

hobbies=[' read ', ' Run ', ' basketball '
# 0 1 2
3, how to use: According to the index value, the index is starting from 0
Print (hobbies[2]) #
Print (hobbies)


l=[' Alex ', ' Male ', [' Oldboy ', 200]]
Print (l[2][1])

Practice
students_info=[[' Egon ', 18,[' play ',]],[' Alex ', 18,[' play ', ' sleep ']
Take out the first hobby of a first student
Print (Students_info[0][2][0])



02 Interacting with users
Input in Python3 will save any content entered by the user into a string type
Name=input ("Please enter your name:") #name = ' Egon '
Pwd=input ("Please enter your password:") #pwd = ' 123 '

Print (Name,type (name))
Print (Pwd,type (PWD))
Print (' =========>1 ')
Print (' =========>2 ')
Print (' =========>3 ')

The Raw_input usage in Python2 is the same as the input of Python3.
Name=raw_input ("Please enter your name:") #name = ' Egon '
Pwd=raw_input ("Please enter your password:") #pwd = ' 123 '


Age=input (' Age>>>: ') #age = ' 18 '
Print (Age,type (age))
Age=int (age)
Print (age > 11)



03 Format output
Name= ' Egon '
Age=18

%s can actually receive any type of value
Print (' My name is%s My age is%s '% (name,age))
Print (' My name is%s My age is%s '% (name,[1,2])

%d can only receive values of type int
Print (' My age is%d '%age)

04 Basic Operators

Print (1 + 2)
x=10
Y=20
Res=x + y
Print (RES)

/yes there are 0 full
Print (10/3)
The floor is divided into only the whole number of parts

Print (10//3)
Print (10//4)


%: Take remainder
Print (10 3)

Print (10 * * 2)


Pwd= ' 123 '
Print (pwd! = ' 123 ')


Compare sizes between lists, only values corresponding to the same location are of the same type
L1=[1, ' A ', 3]
l2=[1,3]

Print (L2 > L1)

Print (> 1.1)

Age=18

# age+=1 #age =age + 1
# age*=10 #age =age*10
Age//=3 #age =AGE//3
Print (age)


Focus:
Chained assignment
d=10
C=d
B=d
A=d

a=b=c=d=10


Cross Assignment
x=100
y=200

Temp=x
X=y
Y=temp
Print (x, y)

X,y=y,x
Print (x, y)


Extracting the value of a variable
l=[' Egon ', ' ASB ', ' WSB ']
X=L[0]
Y=L[1]
Z=L[2]
X,y,z=l

Print (x, y, z)

l=[' Egon ', ' ASB ', ' WSB ', ' LSB ']
X,y,z,a=l
Print (X,y,z,a)
_= ' LSB '
X,_,z,_=l

Print (_)

YJ=[11,22,33,44,55,66,77,88,99,100,200,300]
Mon1,mon2,mon3,mon4,mon5,mon6,*_=yj
Print (Mon1)
Print (Mon2)
Print (Mon3)
Print (MON4)
Print (MON5)
Print (MON6)

Mon1,mon2,*_,mon11,mon12=yj

Print (Mon1)
Print (Mon2)
Print (MON11)
Print (MON12)


For a dictionary, the extracted key
info={' name ': ' Egon ', ' age ':, ' sex ': ' Male '}

X,y,z=info
Print (x, y, z)


Logical operations
Print (Not > 3)

Print (> 3 and 3 > 2)

Print (3 >= 3 and 3 > 2 and ' xxx '! = ' Egon ' and 3 > 2)


Print (False or True)
Print (3 > 3 or 3 > 2 or ' xxx '! = ' Egon ' or 3 > 2)

Print (not 3 > 3 or (not 3 > 2 and ' xxx ' = = ' Egon ' or 3 > 2))

If 3 >= 3 and 3 > 2:
If ' xxx '! = ' Egon ' and 3 > 2:
Print (' ===> ')

Print (3 >= 3 and 3 > 2 and ' xxx '! = ' Egon ' and 3 > 2)

Python Primer One

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.