Concise Python Tutorials

Source: Internet
Author: User
Tags constant in python

Is it enough to simply print "Hello world"? You should want to do more-you want to get some input, then do the operation, and get some output from it. In Python, we can use constants and variables to do these things.

Constants in literal sense

An example of a literal constant is a number like 5, 1.23, 9.25e-3, or as ' this is a string ', ' It ' s a string! ' Such a string. They are called literal, because they have literal meaning-you use their values according to their literal meaning. The number 2 always represents itself, not something else-it is a constant because it cannot change its value. So all of these are called literal constants.

Number

There are 4 kinds of numbers in Python-integers, long integers, floating-point numbers, and complex numbers.

2 is an example of an integer.

Long integers are just larger integers.

3.23 and 52.3E-4 is an example of floating-point numbers. The e tag represents a power of 10. Here, the 52.3E-4 expression 52.3 * 10 -4.

( -5+4j) and (2.3-4.6j) are examples of complex numbers.

String

A string is a sequence of characters. A string is basically a set of words.

I can almost guarantee that you use strings in every Python program, so pay special attention to this section below. Here's how to use strings in Python.

Use single quotes (')

You can use single quotes to indicate strings, just as ' Quote me on this '. All whitespace, i.e., spaces and tabs, are preserved as they are.

Use double quotes (")

The string in double quotes is exactly the same as the string used in single quotes, such as "What ' your name?".

Use triple quotes ("" or "")

With three quotes, you can indicate a string of multiple lines. You can use single and double quotes freely in three quotes. For example:

'''This is a multi-line string. This is the first line.
This is the second line.
"What's your name?," I asked.
He said "Bond, James Bond."
'''

Escape character

Suppose you want to include a single quote (') in a string, so how do you indicate the string? For example, this string is what ' s your name? You're definitely not going to use ' What ' s your name? To indicate it, because Python will not understand where the string starts and where it ends. So you need to specify the single quote rather than the end of the string. You can do this by using an escape character. You use ' to indicate single quotes--notice this backslash. Now you can represent the string as ' What ' your name? '.

Another way to represent this particular string is "What ' your name?", that is, double quotes. Similarly, you can use the escape character when you are using the double quotation mark itself in a double quote string. Alternatively, you can use the escape character \ to indicate the backslash itself.

It is worth noting that in a string, a single backslash at the end of the line indicates that the string continues on the next line, rather than starting a new row. For example:

"This is the sentence.

This is the second sentence.

Equivalent to ' This is the ' the ' the ' the sentence. This is the second sentence.

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.