Learning notes for python3 data type

Source: Internet
Author: User
Tags integer division stdin in python


In Python, each value has a data type, but you do not need to declare the data type of the variable. Python analyzes the type of each variable based on its initial value assignment.
Python has multiple built-in data types. The following are important:

Booleans [Boolean], True [True], or False [False].
Numbers [numeric type] can be Integers [integer] (1 and 2), Floats [floating point] (1.1 and 1.2), Fractions [score] (1/2 and 2/3 ); even Complex Number [plural].
Strings [string type] is a Unicode character sequence, for example, an html document.
Bytes [Byte] and Byte Arrays [Byte array], for example, a jpeg image file.
Lists [list] is an ordered sequence of values.
Tuples [Tuples] are ordered and unchangeable value sequences.
Sets [set] are packages filled with unordered values.
Dictionaries [Dictionary] is an unordered package of key-value pairs.
Of course, there are more types. In Python, everything is an object, therefore, there are types such as module [module], function [function], class [class], method [method], file [file], and even compiled code [compiled code. You have seen such examples: module name, function docstrings, and so on. What we will learn includes the Classes [Classes] in Classes and iterators, and Files [Files] in Files.

Strings [string] and Bytes [byte string] are important and complex, which is sufficient to open up an independent chapter to describe. Let's look at other types first.

The numeric type is awesome. There are too many types to choose from. Python supports both Integer [Integer] and Floating Point [Floating Point] values. There is no type declaration available for differentiation; Python uses whether there are decimal points to distinguish them.


>>> Type (1)
<Class 'int'>
>>> Isinstance (1, int)
True
>>> 1 + 1
2
>>> 1 + 1.0
2.0
>>> Type (2.0)
<Class 'float'>
 

① You can use the type () function to detect any value or variable type. As expected, 1 is of the int type.
② Similarly, you can use the isinstance () function to determine whether a value or variable is of a given type.
③ Add an int and an int to get an int.
④ Adding an int to a float will produce a float. Python forcibly converts the int type to float for addition operations, and then returns a float type result.
The floating point number is accurate to 15 digits after the decimal point.

An integer can be any large.

Python 2 uses different data types for int [integer] and long [long integer. The int data type is limited by sys. maxint, which may vary depending on the platform, but is usually 232-1. Python 3 only has one integer type, and its behavior is a bit like the old long [long integer] type of Python 2. See pep 237 for more details.

Common value calculation

// In Python 2, the operator/usually represents the integer division. However, you can add a special instruction to the code to make it look like a floating point division. In Python 3, the/operator always indicates the floating point division. See pep 238 for more details.
/// The operator executes the floating point division. Even if both the numerator and denominator are int, it returns a float floating point number.
 
>>> 10/2
5.0
>>> 11/2
5.5
//// The operator performs an odd integer division. If the result is a positive number, it can be regarded as an integer toward the decimal place (not rounding), but be careful with this.
>>> 11 // 2
5
// When an integer is divided by a negative number, the result is rounded up to the nearest integer. From a mathematical point of view, because & minus; 6 is smaller than & minus; 5, it is "downward" rounded down. If you want to round the result to & minus; 5, it will mislead you.
>>>-11 // 2
-6
//// The operator does not always return an integer. If the numerator or denominator is float, it will still be rounded to the nearest integer, but the actually returned value will be of the float type.
>>> 11.0 // 2
5.0
// ** The operator indicates the "power of calculation". The Square result of 11 is 121.
>>> 11 ** 2
121
// The % operator returns the remainder after division. The result of dividing 11 by 2 is 5 and the remainder is 1, so the result here is 1.
>>> 11% 2
1

Scores in python


// Introduce the score module
>>> Import fractions
>>> X = fractions. Fraction (1, 3)
>>> X
Fraction (1, 3)
>>> X * 2
Fraction (2, 3)
>>> Fractions. fration (6, 4)
Traceback (most recent call last ):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'fration'
// Automatic scoring
>>> Fractions. Fraction (6, 4)
Fraction (3, 2)
// If the denominator is 0, an error is returned.
>>> Fractions. Fraction (3, 0)
Traceback (most recent call last ):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.3/fractions. py", line 167, in _ new __
Raise ZeroDivisionError ('fraction (% s, 0) '% numerator)
ZeroDivisionError: Fraction (3, 0)
// It is also a form of score calculation.
>>> Fractions. Fraction (0, 3)
Fraction (0, 1)

Trigonometric functions in python


>>> Import math
>>> Math. pi
3.141592653589793
>>> Math. sin (math. pi/2)
1.0
>>> Math. tan (math. pi/4)
0.9999999999999999
In a boolean environment, if the variable value is 0, or 0.00... In this case, it is determined to be False, and other values such as 1 and 0.1 are True.

 

String Analysis

In Python 3, all strings are Unicode-encoded character sequences. (This is a very useful thing)

Similar to the list, you can use the + operator to connect (concatenate) strings.


>>> S = 'go deep into Python'
>>> Len (s)
9
>>> S [0]
'Shen'
>>> S + '3'
'Go deep into python3'

Python 3 supports formatting values into strings. There can be very complex expressions. The most basic usage is to insert a value into a string using a single placeholder (placeholder.


>>> Username = 'Mark'
>>> Password = 'zspwd'
>>> '{0}'s password is {1}'. format (username, password)
File "<stdin>", line 1
'{0}'s password is {1}'. format (username, password)
         ^
SyntaxError: invalid syntax
>>> '{0} \'s password is {1}'. format (username, password)
"Mark's password is zspwd"
Python string slicing (equivalent to the substr function of some programs)


>>> A_string = 'My alphabet starts where your'
>>> A_string [3]
'A'
>>> A_string [3:]
'Alphabet starts where your'
>>> A_string [: 5]
'My Al'
>>> A_string [3: 11]
'Alphabet'
>>> A_string [3:-3]
'Alphabet starts where Y'
The slice position starts from 0 and is switched to a position that does not contain the second parameter (this is different from that of many programming languages)

Python3 is better to use UTF-8 encoding

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.