Python3 Basic 3--data Type--data operation--expression if-else-while-for

Source: Internet
Author: User
Tags arithmetic

Python3 data Type 1.1 numbers for example: 1,2,3,4, such as 1.2 int (integer) on 32-bit machines, the number of integers is 32 bits, the value range is -2**31~2**31-1, that is -2147483648~2147483647
On a 64-bit system, the number of integers is 64 bits and the range is -2**63~2**63-1, that is, -9223372036854775808~9223372036854775807 1.2.1 Long is different from C language, Python's long Integer does not refer to the positioning width, i.e. python does not limit the size of long integer values, but in fact, because of the limited machine memory, we use a long integer value can not be infinite.

1.3 Float (floating-point) rational number: real, imaginary. A floating-point number is used to process real numbers, which are numbers with decimals. Similar to the double type in C, accounting for 8 bytes (64 bits), where 52 bits represent the bottom, 11 bits represent the exponent, and the remaining one represents the symbol. 1.4 Complex complex numbers are composed of real and imaginary parts, the general form is X+yj, where x is the real part of the complex number, and Y is the imaginary part of the plural, where x and y are real numbers. 1.5 Boolean True or false true or False1 or 0 1.6 string "Python3" 1.6.1 string concatenation ("+") the string in Python is represented in the C language as a character array, and each time a string is created it needs to open a contiguous space in memory. And once you need to modify the string, you need to open up space again, the amount of data can be so dry, if the amount of data is very large, there is no need to copy the same data separately. 1.6.2 string formatted output (% placeholder, {} placeholder, format () method) name = "Python3"
Print ("This is%s"% name) Note:%s, string,%d, number,%f, floating-point {name = "Python3"} print (' I\ ' m {},{} '. Format (' Python3 ', ' Welcome to My space! ')) 1.7 Python data Type list example: Names = [' Python2 ', ' python2.7 ', ' python3 ', ' python3.6 ']
Print (names) 1.7.1 select query Slice print (names[-4:]) print (names[:]) print (names[:-1]) print (names[::-1]) print (Names[1:2] ) Print (Names.index (' Python2 ')) print (Names.count (' python3.6 ')) 1.7.2 Insert Insert Names.append (1) Names.insert (1, "PTH") 1.7.3 update names[1]= "py" names[:]= "py" 1.7.4 del delete del names[1]del namesnames.pop () names.pop (1) names.remove (names[ 1]) Names.remove ("Python2") 1.8 Python data type tuple (tuple) Example: Names = (' Python2 ', ' python2.7 ', ' python3 ', ' python3.6 ', ' python3.6 ') names.count ("py") # # # Statistics py occurrences of print (Names.index (' python2.7 ')) # # # #返回下标值 1.9 python data Type list (dictionary) Example: names = {' Python2 ', ' python2.7 ', ' python3 ', ' python3.6 ', ' python3.6 '} stu = {
' Num1 ': ' Tom ',
' num2 ': ' Lucy ',
' num3 ': ' Sam ',
}
Print (Stu.clear ())

#输出: None

Stu = {
' Num1 ': ' Tom ',
' num2 ': ' Lucy ',
' num3 ': ' Sam ',
}
STU2 = Stu.copy ()
Print (STU2)

name = [' Tom ', ' Lucy ', ' Sam ']
Print (Dict.fromkeys (name))
Print (Dict.fromkeys (name,25)) #指定默认值


#输出: {' Tom ': None, ' Lucy ': None, ' Sam ': none}
# {' Tom ': +, ' Lucy ': +, ' Sam ': 25}

Stu = {
' Num1 ': ' Tom ',
' num2 ': ' Lucy ',
' num3 ': ' Sam ',
}
Print (Stu.get (' num2 '))

Stu = {
' Num1 ': ' Tom ',
' num2 ': ' Lucy ',
' num3 ': ' Sam ',
}
Print (Stu.keys ())

Stu = {
' Num1 ': ' Tom ',
' num2 ': ' Lucy ',
' num3 ': ' Sam ',
}
Print (Stu.valuses ())

#输出: Lucy

Stu = {
' Num1 ': ' Tom ',
' num2 ': ' Lucy ',
' num3 ': ' Sam ',
}
Print (Stu.items ())

#输出: Dict_items ([' num2 ', ' Lucy '), (' num3 ', ' Sam '), (' Num1 ', ' Tom ')])

Stu = {
' Num1 ': ' Tom ',
' num2 ': ' Lucy ',
' num3 ': ' Sam ',
}
Print (Stu.keys ())

#输出: Dict_keys ([' num3 ', ' num1 ', ' num2 '])

Stu = {
' Num1 ': ' Tom ',
' num2 ': ' Lucy ',
' num3 ': ' Sam ',
}
Name = Stu.pop (' num2 ')
Print (Name,stu)

#输出: Lucy {' num1 ': ' Tom ', ' num3 ': ' Sam '}

Stu = {
' Num1 ': ' Tom ',
' num2 ': ' Lucy ',
' num3 ': ' Sam ',
}
Name = Stu.popitem ()
Print (Name,stu)

#输出: (' num2 ', ' Lucy ') {' num3 ': ' Sam ', ' num1 ': ' Tom '}

Stu = {
' Num1 ': ' Tom ',
' num2 ': ' Lucy ',
' num3 ': ' Sam ',
}
Name = Stu.setdefault (' NUM5 ')
Print (Name,stu)

#输出: None {' num1 ': ' Tom ', ' num2 ': ' Lucy ', ' num5 ': None, ' num3 ': ' Sam '}

Stu = {
' Num1 ': ' Tom ',
' num2 ': ' Lucy ',
' num3 ': ' Sam ',
}
Stu.update ({' num4 ': ' Ben '})
Print (STU)

#输出: {' num2 ': ' Lucy ', ' num3 ': ' Sam ', ' num1 ': ' Tom ', ' num4 ': ' Ben '} PS: Note the difference between copy and deepcopy, the difference between update and SetDefault. Second, python3 data arithmetic arithmetic operation:
+
-
*
/
%
**
//

Comparison operation:
==
!=
<>
<
>
<=
>=

Assignment operation:
=
+=
-=
*=
/=
%=
**=
//=
Logical operation:
and
Or
Not

Member Operations:
Inch
Not in


Identity operation:
Is
is not

Bit operations:
& Exchange with
|| And OR
^ XOR or
~ Take counter
>> left Move
<< Right Move


#!/usr/bin/env python
A = 60 # 60 = 0011 1100
b = 13 # 13 = 0000 1101
c = 0

c = A & B; # 12 = 0000 1100
Print "line 1-value of C is", C

c = A |        b # 61 = 0011 1101
Print "line 2-value of C is", C

c = a ^ b; # 0011 = 0001 #相同为0, different 1
Print "line 3-value of C is", C

c = ~a; #-61 = 1100 0011
Print "line 4-value of C is", C

c = a << 2; # 240 = 1111 0000
Print "line 5-value of C is", C

c = a >> 2; # 15 = 0000 1111
Print "line 6-value of C is", c three, Python3 control loop expression 3.1 if-else scene One, user login verification # Prompt for user name and password
# Verify user name and password
# If an error occurs, the output user name or password is incorrect
# If successful, the output is welcome, xxx!

#!/usr/bin/env python
#-*-Coding:encoding-*-

Username = input ("Enter user name:")
Password = input ("Enter password:")

If username = = "Python3" and Password = = "Python3":
Print ("Welcome,%s!",% username)
Else
Print ("Error username or password")

#!/usr/bin/env python
#-*-Coding:encoding-*-
Import Getpass

Name = input (' Please enter user name: ')
PWD = Getpass.getpass (' Please enter password: ')

If name = = "Python3" and pwd = = "Python3":
Print ("Welcome,%s! ",% username)
Else
Print ("User name and Password error") PS: note the indent format. 3.2 If elif else scenario two, guess age game
#!/usr/bin/env python
#-*-Coding:utf-8-*-

My_age = 18

user_input = Int (input ("Input your guess num:"))

if user_input = = My_age:
Print ("Congratulations, you got it!")
Elif User_input < my_age:
Print ("Oops,think bigger!")
Else
Print ("Think smaller!") 3.3 While count = 0
While True:
Print ("Always loops 99 Times", Count)
Count +=1
if count = = 100:
Print ("Test pass")
Break 3.4 for demand one: or the above program, but encountered less than 5 cycles will not go, jump directly into the next cycle

For I in range (10):
If i<5:
Continue #不往下走了, go directly to the next loop
Print ("Loop:", i)
Demand two: Or the above program, but encountered more than 5 cycle times will not go, direct exit

For I in range (10):
If i>5:
Break #不往下走了, jump out of the loop directly
Print ("Loop:", i)

Python3 Base 3--data Type--data operation--expression if-else-while-for

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.