"Python programming from getting started to practicing" chapter II _ Variables and simple data types

Source: Internet
Author: User
Tags arithmetic

What is a variable?

Example:
>>> message = "hello,python!" >>> print (message) hello,python!

The message here is the variable. You can modify the value of a variable at any time in your program, and Python will always record the latest value of the variable.

what are the rules for variable commands? Variables can only contain alphanumeric underscores. Variable names cannot contain spaces, but you can use underscores to split the words in them. Do not include Python keywords and function names as variable name variable names should be simple and descriptive with lowercase characters L and capital letter O, because they may be mistaken for numbers 1 and 0.what is a string? A string is a series of characters, used in Pythonenclosed in single or double quotation marks. Because there are two ways, you can flexibly include quotes and apostrophes in strings.
' I told my friend, ' Python is my favorite language! ' " The language ' Python ' is named after Monty Python and not the snake. " One of the Python ' s strengths is its diverse and supportive community. "

A few basic methods

1.title () Capitalize first character

>>> name = "Liu Bin" >>> print (Name.title ()) Liu Bin

2.upper () All CAPS conversion

>>> name = "Liu Bin" >>> print (Name.upper ()) Liu Bin

3.lower () All lowercase conversions

>>> name = "Liu Bin" >>> print (Name.lower ()) Liu Bin

How do you fit strings together?

A simple "+" plus sign can be implemented.

>>> first_name = "Bin" >>> last_name = "Liu" >>> full_name = last_name + "" + first_name>> > Print (full_name) Liu Bin

Tabs and line breaks

tab: \ t

>>> print ("python") python>>> print ("\tpython") python

Line break: \ n

Print ("pythonphp") pythonphp Print ("python\nphp") pythonphp

How do I delete blanks?

Delete Trailing blanks: Rstrip () Delete header blank: Lstrip () Remove both ends blank: strip ()

Operation?

Integer subtraction

>>> 2 + 35>>> 3-21>>> 2 * 36>>> 3/21.5

exponentiation operation

>>> 3 * * 29>>> 3 * * 327>>> 10 * * 61000000

Support for parentheses Precedence

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

Floating point Arithmetic

>>> 0.1 + 0.10.2>>> 0.2 + 0.20.4>>> 2 * 0.10.2>>> 2 * 0.20.4

The result of floating-point arithmetic may be indeterminate, and all languages will have this problem, not to worry, there are workarounds

>>> 0.2 + 0.10.30000000000000004>>> 3 * 0.10.30000000000000004

non-string conversion to a string?

Examples of STR () errors:
>>> age = 23>>> message = "Happy" + age + "Rd birthday!" Traceback (most recent):  File "<pyshell#79>", line 1, in <module>    message = "Happy" + Age + " Rd birthday! " Typeerror:must is str, not int

So use STR () to convert

>>> message = "Happy" + str (age) + "rd birthday!" >>> print (message) Happy 23rd birthday!

Notes

Use # Comments in Python, wrap comments with three quotes

# Say hello to everyone print ("Hello Python people!")

  

"Python programming from getting started to practicing" chapter II _ Variables and simple 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.