python of basic grammar -1

1.python comments:

# Annotated using the single line of code.

# This is a comment line 

# Print "the Hello Word"

With three lines of code marks "" "" "", '' '' '' is annotated.

# For 

example: "" "The first line of the comment 
    annotation second row 
    comment line 3" ""

Second, the computer and user interaction

 

** 1, what is the interaction with the user **

    The program waits for the user to enter some data, and then completes the program for the user feedback information

 

** 2, why the program to interact with the user **

    In order for the computer to interact with the user like a man

 

** 3. How **

 

* Python3 in

 

  Nothing will be input by the user input are stored as a string type

# For example 

name = the INPUT ( " Please enter your user name: " )
   >>> Please enter your user name: Egon
   >>> Print (name) 
  Egon
   >>> Print (name, of the type (name)
   >>> ( ' Egon ' , <type ' STR ' >)

In python2 the input user must enter a well-defined data types, user input on what type of memory to be what type.

IS not defined # because there is no input of data indicating the type of the error. Correct input examples are as follows:
   
X = INPUT >>> ( " Input: " ) 
  Input: ' Egon ' 
  >>> X, type (X) 
  ( ' Egon ' , <type ' STR ' > )
  
  
   >>> X = INPUT ( " Input: " ) 
  input: [ l, 2,3 ]
   >>> X, type (X) 
  ([ . 1, 2,. 3], <type ' List ' >)

# Python2 the same in the input python3 raw_input and any type of user input data are stored to a string type.

>>> x= raw_input(":")
:shanghai
>>> x,type(x)
('shanghai', <type 'str'>)
>>> y = raw_input(":")
:[1,2,3,5,67]
>>> y,type(y)
('[1,2,3,5,67]', <type 'str'>)

Third, the output format:

Placeholder:% s% d and 

1,% s Usage

>> name = raw_input(":")

:andy

>>> age = input(":")

:22

>>> print ('my name is %s my age is %s' %("andy",22))

my name is andy my age is 22

#% S% d and the difference between:

name=input('please input your username:')
age=input('please input your age:')

print('my name is %s my age is %d' %('egon',18))
print('my name is %s my age is %s' %('egon',18))
print('my name is %s my age is %s' %('egon',[1,2,. 3])) (Print% S may receive any type of value#
' My name My Age IS IS S% D% ' % ( ' Egon ' , ' XXX ' )) # % D can receive a digital type

Fourth, the data type:

Data type meanings:

Data types can be disassembled to be understood that the data recording state is used, and the type indicates the different status should go represented by different data types.

1. Integer int

usage:

Int = A >>> (10 )
 >>> Print (A, type (A)) 
( 10, <type ' int ' > )
 # used in arithmetic operations. .

2. float float

usage:

>>> salary = 8.2
>>> print (salary,type(salary))
(8.2, <type 'float'>)

# In order to facilitate memory we can collectively referred to as the above two types of numeric types.

Added: complex, long integer

n = 1 + 2j

type(n)

n.real

n.imag

 

# Longs   only python2 in only !!! 

n = 123

# int

n = 12221312321313213213213123

# long

# 12221312321313213213213123212312L

n = 1L

#Define long integer

 

3. String str

Character Type: str

Role: data records describe the nature of such person's name, gender, home address, company profile

Definition: quotes sequentially left to right, contains a character, quotes may be a single and double quotation marks, three marks

usage:

>>> name = "shanghai"
>>> print (name,type(name))
('shanghai', <type 'str'>)

# Three primers can save multiple lines, and can be treated as a comment in the absence of received variable name

 

 # The reason why there are three ways, in order to prevent the text string values ​​also require the use of quotation marks and inconvenience.

3.1 string splicing:

>>> x = "hello"
>>> y = "oldboy"
>>> print (x+y)
hellooldboy
>>> print (x*5)
hellohellohellohellohello

# Emphasize:

# 1, may be added between the strings (between different data types are not added)

# 2, a character string adding application memory and copy the new strings are added to the new space, the efficiency is not high

 print('my name is '+'egon'+' my age is '+'18')

# 3, the string can also do multiplication

 print('hello'*10)

 print('='*100)

 

## list type list

Action: a recording / multiple values ​​can be easily taken out of the specified value.

Definition: value [] of any of the plurality of spaced apart by a comma

List = >>> [1,2,3,45,6 ,]
 >>> Print (List [. 3 ])
 45 
Note # value of the index is zero-based.

Dictionary type dict

 

#作用:记录多个key:value值,优势是每一个值value都有其对应关系/映射关系key,而key对value有描述性的功能。

 

#定义:在{}内用逗号分隔开多个key:value元素,其中value可以是任意的数据类型,而key通常应该是字符串类型。

>>> info = {'name':'egon','sex':'male','age':18}
>>> print (info['name'])
egon

#采用kv键值对,通过键 key 来读取相应的值。

布尔类型bool

布尔值:Ture , False 

判断事物的对错(真假)。

定义:

 tag=True # tag=bool(True)

 tag=False

 print(type(tag))

# 一般情况下我们不是直接操作的布尔值而是通过判断去得到布尔值

# print(age > 20)

# print(age >= 20)

 

# age=18

# ==比较的是值

# print(age == 18)

# is:比较的是id是否相等

# 强调:id相等值一定相等,id不等但是值仍然可以相等

 

# x=1

# y=x

# print(x is y)

 

tag=True

print(id(tag))

 

res=3 > 1

print(id(res))

 

res2=1 < 10

print(id(res))

 

# 布尔值在内存中就两个值,不会反复的开辟新的内存空间存放

基本运算符

#算术运算

#算术运算
res = 1+5
print (res)
print (1+8)
print(10/3) #结果保留小数
print(10//3)#结果保留整数部分
print(10%3)#取余数

#比较运算:

==  等于

!=  不等于

>  大于

<  小于

>= 大于等于

<=  小于等于

 

#数字之间比较大小
a = 10
b = 7
print(a>b)

#字符串只能和字符串比较大小

m1 = "hello"
m2 = "z"
print(m1>m2)

# A-Z ,a-z , 最大的是:z , 最小的是:A

 

#列表只能与列表比较大小(按照对应位置的值依次比较,对应位置的值必须是相同的类型)

 

l1=[1,2,3]
l2=[10,]
print(l2 > l1)
l3=[10,2,'b',3]
l4=[10,2,'b','c']
print(l3 > l4)

# 赋值运算

a = 11
b = 19

# 增量赋值

age += 1 #age=age+1
print(age)

# 链式赋值

x=100
y=x
z=x

x=z=y=100
print(id(x),id(y),id(z))

# 交叉赋值

>>> m = 100
>>> n = 99
>>> m,n = n,m
>>> print (m,n)
(99, 100)

# 解压赋值

>>> s = [11,22,33,55,66]
>>> a1,a2,a3,a4,a5 = s
>>> print(a1,a2,a3,a4,a5)
(11, 22, 33, 55, 66)
>>> a1,_,_,_,a5 = s
>>> print(a1,a5)
(11, 66)
>>> print(_)
55
>>> a1,*_,a5 = s #只有在python3才能使用。

#逻辑运算

 

常见的逻辑运算符:and , or , not

 

 

 

and : 连接左右两个条件,只有在两个条件同时成立的情况下最终结果才为True.

print(age > 18 and age < 26 and sex == 'female' and 1 > 3)

or : 连接左右两个条件,但凡有一个条件成立最终结果就为True.

print(1 > 3 or 2 > 4 or 'x' == 'y' or 1==1)

not : 取反

print(not 1 > 3)
print(not (1 > 3 or 2 > 4 or 'x' == 'y' or 1==1))

res=(3>4 and 4>3) or (1==3 and ('x' == 'x' or 3 >3))
print(res)

 

 

 

 

Guess you like

Origin www.cnblogs.com/chendaodeng/p/11116436.html