python02-operators and basic data types

Source: Internet
Author: User
Tags arithmetic ming

One, Operator:

1. Arithmetic Operation:

2. Comparison operation:

3. Assignment Operation:

4. Logical Operation:

Attention:

No () is a priority, first ()

No () No priority, look backwards,

According to the smooth execution, if the first set up, followed by or will not be seen, the output is true, if not, the output is Fales

1 if 1 = = 1 or 1 > 2 and 1 = = 4:2     print (' correct ') 3 else:4     print (' ERROR ')

Output is True

1 if 1 = = 3 or 1 < 2 and 1 = = 4:2     print (' correct ') 3 else:4     print (' ERROR ')

Output is Fales

5. Member Arithmetic:

For example:

1 a=' Xiao Ming to go to school on the first day, very happy, and teachers and classmates get along happily '2if' xiaoming ' inch A: 3    Print (' sensitive characters ') 4 Else : 5    Print (a)

Second, the data type:

1, Integer: int represents

Create integer: a=123

A=int (123)

Strings can be converted to numbers:

1 age=' 2 new_age=int (age)3print(new_ Age

To see what type it belongs to:

A=123

Print (Type (a))

If int is an integer

On a 32-bit machine, the number of integers is 32 bits and the value range is -2**31~2**31-1, which is -2147483648~2147483647
On a 64-bit system, the number of integers is 64 bits and the value range is -2**63~2**63-1, which is -9223372036854775808~9223372036854775807

2. Boolean value: True fales

BOOL Representative

Create:
A=bool (2)
Print (a)

Number conversion Boolean value only 0 is Fales, the rest is true

String conversions only "" spaces are fales and the rest are true

3, String: STR representative

Single quote double quote three quotes can be "Hello Kate" has a character composed of

Created: a= "Hello Kate"

A=str ("Hello Kate")

The number is converted to a string:

1 age=192 new_age=str (age)

string concatenation: A, "+" to add the way

1 ' Alex ' ' female ' 3 new_str = name + gender4print(NEW_STR)

String formatting:

B, placeholder%s string%d number is typically included with%s (numeric string)

# name = ' My name is Li Jie, sex:%s, I'm%s this year, I'm lying! # new_str = name% (' male ', +,) # print (NEW_STR)

Determines whether a character is in:

For example:

1 a=' Xiao Ming to go to school on the first day today, very happy, and teachers and classmates get along happily 'if' xiaoming  ' in A:3    print(' sensitive characters ')else: 5    print(a)

Common functions:

A remove whitespace: Strip line break also removed

1 "  "2print(val)3#  about 4 new_val = Val.lstrip ()#  left 5#  right 6print(new_val )

Split B: Split

Separators: spaces, | etc.

1 " alex|sb123|9 "   2  v = user_info.split ('| ' )  3  v = user_info.split ('| ', 1) divide only the first one from the left to split 4  v = user_info.rsplit (', 1   5  print(v)

C Length: Len calculates length by character

val = "Li Jie sb"
v = Len (val)
Print (v)

D index: []

The index position is calculated starting from 0, taking one character:

val = "Li Jie sb"
v = val[0]
Print (v)

Loop output:

1 val = input ('>>>')2  i = 03 While   i < Len (val):4  Print (Val[i]) 5   i + = 1

E-Slice: Takes a range of characters

[0:2]

[0:10:2] The last number stands for less often, jumping away, across

1 ' My name Li Jie, sex 18, I am this year old, I like swimming! ' 2 Print (name[0]) 3 Print (Name[0:2]) 4 Print (Name[5:9]) 5 Print (name[5:]) 6 Print (Name[5:-2]) 7 Print (name[-2:])

4. Lists: List representative

Created: a=[' DDF ', ' jiff ', ' Liujie '

A=list ([' DDF ', ' jiff ', ' Liujie '])

Common functions:

For example:

1a=['ADKFI','God carved Jianou','Ijof', 1234] 2R=A[0]3 Print(r) Index4t=Len (a)5 Print(t) length6A.append (234)7 Print(a) additional8A.insert (0,'Sjij')9 Print(a) InsertTenA.remove (1234)  One Print(a) deletion A delA[2] Delete by index - Print(a) -a[2]='FFF'To modify a value for re-assignment the Print(a)

For loop: Automatic sequential looping

1  for inch A: 2 Print (item) 3 Break   4 Continue

6, Dictionary: (unordered) DICT representative:

Create v={' name ': ' Sun ',

' pwd ': ' 123 '}

V=dict ({,,,,,,,})

A dictionary is a combination of n key-value pairs. Key-value pair =key+value

For example:

1X=5'name':'Sun',2    'pwd':'123123'}3 #Get by index4n=v['name']5 Print(n)6 #N. An increase, absence, addition, modification7v[' Age']=198 Print(v)9 ## DeleteTen #del v[' name '] One #Print (v) A  - #Loop key only - #For item in V.keys (): the #print (item) -  - #Loop only Value - #For item in V.values (): + #print (item) - ## All Loops Item + #For Key,val in V.items (): A #print (key,val)

python02-operators and basic data types

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.