Python Learning notes 1_ variables and data types

Source: Internet
Author: User

1.1 Variables
    • Variable names can only contain letters, numbers, and underscores, and variable names may begin with a letter or underscore, but cannot begin with a number
    • Do not use Python keywords and function names as variable names
    • Use the lowercase l and the capital Letter o sparingly, as they may be mistaken for numbers 1 and 0.
# Variable name message " Hello Python world! " Print  "Hello Python Crash Course world! " Print (message)

1.2 String 1. String Case Change
# Capitalize first letter " Ada Lovelace " Print (Name.title ()) # string All uppercase Print (Name.upper ()) # string All lowercase Print (Name.lower ())

Method Lower () is useful when storing data. Many times, you can't rely on users to provide the correct case, so you need to convert the strings to lowercase before storing them. You need to display this information later, and then convert it to the most appropriate case.

2. Merging (stitching) strings

Python uses a plus sign (+) to combine strings. The following example uses the + to merge first_name, spaces, and last_name to get the full name

" Ada "  "Lovelace  " "" + last_name print (Full_name)

3. Use tabs or newline characters to add blanks

Add a tab to the string, use the character combination \ t

Print ("Python") Print ("\tpython")

To add a line break to a string, use a combination of characters \ n

Print ("languages:\npython\nc\njavascript")

1.3 Numbers 1. Integer

In Python, a plus (+) minus (-) multiplication (*) is performed on an integer

a=2b=3c=a*bprint("c=", c)

Python uses two multiplication sign to represent a exponentiation operation

2. Floating point number

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, but be aware that the result contains a number of decimal digits that may be indeterminate (as in the example below), but this problem exists in all languages.

a=0.2b=0.1c=a+bprint("c=", c)

3. Use the function str () to avoid type errors

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

Age ="" "Rd birthday! " Print (message)

If they run, they will be found to raise an error, which is a type error, meaning Python does not recognize the information you are using.

Python knows that this variable may represent a value of 23, or a character of 2 and 3. Like above, when using integers in strings, you need to explicitly show that you want Python to use this integer as a string. To do this, you can call the function str (), which lets Python represent non-string values as strings:

Age ="" "Rd birthday! " Print (message)

Python Learning notes 1_ variables and data types

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.