Beginners Learn Python's object types and operations

Source: Internet
Author: User

Variables in Python do not need to be declared. Each variable must be assigned before it is used, and the variable will not be created until the variable is assigned.

In Python, a variable is a variable, it has no type, and what we call "type" is the type of object in memory that the variable refers to.

The equals sign (=) is used to assign a value to a variable.

1 Assigning values to variables

1.1 Assigning a single variable

>>> name= "python" >>> print (name)

Python

More than 1.2 variable assignment values

>>> name=names= "python" >>> print (name,names) Python python

More than 1.3 variables assign multiple objects

>>> a,b=1,2>>> Print (A, B) 1 2

2 Use of variables

2.1 Working with Data Objects directly

>>> print (1+2*3/2) 4.0>>> print (' Python is my favorite language ') Python is my favorite language

One drawback of this usage is that when we need to call data expressions or long strings repeatedly, it can be a great inconvenience, and if complex data operations are involved, then the expression will be repeated, wasting time and reducing efficiency.

2.2 Using variables instead of data objects

Count=1+2*3/2print (a) sentence= ' Python is my favorite language ' print (b)

In this way, the complex operation results are preserved, the repetition is avoided, the complex text is simplified, the readability is enhanced, and the variables are like these expressions, the proxy of complex text, the surrogates, and the convenience of the programmer's operation.

Tips: Numeric use does not require quotation marks, variable use does not require quotation marks, string use requires quotation marks

Standard data types

There are six standard data types in the Python3:

    • Number (numeric)

    • String (String)

    • List (lists)

    • Tuple (tuple)

    • Sets (collection)

    • Dictionary (dictionary)

numeric types and strings

In Python, because there is no data type declaration, we cannot use int and string to distinguish between numbers and strings, but we can use an intuitive way to manage and use them, that is, numbers without quotes, strings quoted, strings being encoded in order to be stored

>>> 1+12>>> ' 1 ' + ' 1 ' 11 '

Can be understood as

Without quotation marks, the interpreter knows what we want to convey is a number, so we use a binary conversion and then add and subtract, and the resulting value is converted into decimal.

After adding the quotation marks, the interpreter knows that we convey a string, it is equivalent to a small map, the shape is 1, it is necessary to compare the code table, find the corresponding position of the number of ' 1 ', and then convert to binary, and then 1 the same way, the addition is from left to right side, and then in the Code table, Find the appropriate map to print out


1 numbers

In programming, numbers are often used to record game scores, to represent visual data, to store Web application information, and so on. Python handles them differently based on the usage of numbers. Given the simplest use of integers, let's take a look at

How Python manages them.

1.1 Integers

In Python, you can perform a plus (+) minus (-) multiplication (*) (*) Power (*) operation on an integer.

>>> 1+23>>> 2-11>>> 2*12>>> 2/12.0>>> 2**38

Python also supports the order of operations, so you can use multiple operations in the same expression. You can also use parentheses to modify the order of operations so that Python performs the operation in the order you specify

>>> 2+3*414>>> (2+3) *420

1.2 Floating point

Python is called a number with a decimal point as a floating point. Most programming languages use the term, which points to the fact that the decimal point can appear anywhere in the number. Each programming language must be carefully designed to properly

Handle floating-point numbers to ensure that the number behaves normally regardless of where the decimal point appears.

To a large extent, the use of floating-point numbers does not have to consider their behavior. Just enter the numbers you want to use, and Python will usually handle them the way you expect them to:

>>> 0.1+0.10.2>>> 0.2-0.10.1>>> 2*0.10.2>>> 2/0.120.0

Use function str () to avoid type errors

You often need to use the value of the variable in the message. For example, suppose you want to wish someone a happy birthday, you might write code similar to the following:

Birthday.pyage = 23message = "Happy" + age + "Rd birthday!" Print (message) Traceback (most recent call last): File ' birthday.py ', line 2, in <module>message = ' Happy ' + Age + ' r D birthday! " Typeerror:can ' t convert ' int ' object to str implicitly

This is a type error, meaning Python does not recognize the information you are using. In this example, Python finds you used a variable with a value of integer (int), but it does not know how to interpret the value (see). Python knows

This variable may represent a value of 23, or a character of 2 and 3. When you use integers in a string like the above, you need to explicitly indicate that you want Python to use this integer as a string. To do this, the function str () can be called,

It lets Python represent non-string values as strings:

Age = 23message = "Happy" + str (age) + "rd birthday!" Print (message) Happy 23rd birthday!

In this way, Python will know that you want to convert the value 23 to a string, and then display the characters 2 and 3 in the Birthday blessing message. After the above processing, the message you expect will be displayed without raising an error



This article from the "11818322" blog, reproduced please contact the author!

Beginners Learn Python's object types and operations

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.