Python3 basics 3 -- Data Type -- data operation -- Expression if, python33 --

Source: Internet
Author: User

Python3 basics 3 -- Data Type -- data operation -- Expression if, python33 --
1. For python3 data type, 1.1 numbers, such as 1.2, and int (integer) are on 32-bit machines. The value range is-2 ** 31 ~ 2 ** 31-1, I .e.-2147483648 ~ 2147483647
In a 64-bit system, the number of digits of an integer is 64-bit, and the value range is-2 ** 63 ~ 2 ** 63-1, that is,-9223372036854775808 ~ 9223372036854775807 1.2.1 long (long integer) is different from the C language. The Length Integer of Python does not specify the Bit Width, that is, Python does not limit the size of the long integer value, but in fact due to limited machine memory, the long integer value we use cannot be infinitely large.

1.3 float (float type) rational number: real number, virtual number. Floating point numbers are used to process real numbers, that is, numbers with decimals. Similar to the double type in C, it occupies 8 bytes (64-bit), where 52 bits represent the bottom, 11 bits represent the index, and the remaining one represent the symbol. 1.4 A complex number is composed of a real number and a virtual number. Generally, x + yj is used. x is the real number of a complex number, and y is the virtual number of a complex number, here, both 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 as a character array in C, each time you create a string, you need to open up a continuous empty block in the memory. Once you need to modify the string, you need to open up space again. This can be done if the data volume is small. If the data volume is very large, there is no need to copy the same data separately. 1.6.2 string formatting output (% placeholder, {} placeholder, format () method) name = "python3"
Print ("This is % s" % name) Note: % s, String, % d, number, % f, floating Point Number {name = "python3"} print ('I \'m {},{}'. format ('python3', 'Welcome to my space! ') 1.7 python data type list (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 []) print (names. index ('python2') print (names. count ('python3. 6 ') 1.7.2 insert into 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 tuples (tuple) Example: names = ('python2', 'python2. 7', 'python3', 'python3. 6', 'python3. 6') names. count ("py") ### count the number of times that py appears print (names. index ('python2. 7 ') ### return the list (dictionary) of the python data type with the following standard value: names = {'python2', 'python2. 7', 'python3', 'python3. 6', 'python3. 6'} stu = {
'Num1': 'Tom ',
'Num2': 'Lucy ',
'Num3': 'Sam ',
}
Print (stu. clear ())

# Output: 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) # specify the default value


# Output: {'Tom ': None, 'Lucy': None, 'Sam ': None}
# {'Tom ': 25, 'Lucy': 25, '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 ())

# Output: Lucy

Stu = {
'Num1': 'Tom ',
'Num2': 'Lucy ',
'Num3': 'Sam ',
}
Print (stu. items ())

# Output: dict_items ([('num2', 'Lucy '), ('num3', 'Sam'), ('num1', 'Tom ')])

Stu = {
'Num1': 'Tom ',
'Num2': 'Lucy ',
'Num3': 'Sam ',
}
Print (stu. keys ())

# Output: dict_keys (['num3', 'num1', 'num2'])

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

# Output: Lucy {'num1': 'Tom ', 'num3': 'Sam '}

Stu = {
'Num1': 'Tom ',
'Num2': 'Lucy ',
'Num3': 'Sam ',
}
Name = stu. popitem ()
Print (name, stu)

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

Stu = {
'Num1': 'Tom ',
'Num2': 'Lucy ',
'Num3': 'Sam ',
}
Name = stu. setdefault ('num5 ')
Print (name, stu)

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

Stu = {
'Num1': 'Tom ',
'Num2': 'Lucy ',
'Num3': 'Sam ',
}
Stu. update ({'num4': 'ben '})
Print (stu)

# Output: {'num2': 'Lucy ', 'num3': 'Sam', 'num1': 'Tom ', 'num4': 'ben'} PS: note the differences between copy and deepcopy, and between update and setdefault. Ii. python3 data arithmetic operation:
+
-
*
/
%
**
//

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

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

Member calculation:
In
Not in


Identity calculation:
Is
Is not

Bitwise operation:
& Submit
| And or
^ Exclusive or
~ Invert
> Move left
<Move right


#! /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; #49 = 0011 0001 # Same as 0, different as 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 3, python3 control loop expression 3.1 if-else scenario 1. user login verification # prompt to enter the user name and password
# Verify the user name and password
# If an error occurs, the output username or password is incorrect.
# If yes, output welcome, XXX!

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

Username = input ("input username :")
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 ('enter your Username :')
Pwd = getpass. getpass ('Enter the password :')

If name = "python3" and pwd = "python3 ":
Print ("Welcome, % s! ", % Username)
Else:
Print ("Incorrect username and password") PS: note the indent format. 3.2 if elif else scenario 2. Age play
#! /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 ("99 cycles all the time", count)
Count + = 1
If count = 100:
Print ("tested ")
Break 3.4 for requirement 1: The above program is used, but the number of cycles smaller than 5 will not go away, directly jump into the next loop

For I in range (10 ):
If I <5:
Continue # Don't go down, go directly to the next loop
Print ("loop:", I)
Requirement 2: The above program is used, but the process does not go if the number of cycles is greater than 5. Exit directly.

For I in range (10 ):
If I> 5:
Break # Jump out of the loop without going down
Print ("loop:", I)

Related Article

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.