Python Learning Notes-variables and simple data types

Source: Internet
Author: User

Variables in python Note: Variables do not have a specified type, but must be initialized. first, the use of variables1. Variable names can only contain alphanumeric underscores. Cannot start with a number. For example: 3variable is wrong. 2. Variable names cannot contain spaces, but can be separated by underscores. For example: Greeting_message is possible, but the variable name greeting message is wrong. 3. You cannot use the keyword and function names in Python as variable names, that is, you do not need to keep the words used by Python for special purposes as variable names. For example, print. 4. Variable names should be short and descriptive. For example: Using name is better than using N, using Student_name is better than SN. Note: Python's variable names are identified in lowercase letters as much as possible, and Python is a case-sensitive programming language. Second, StringPython is a string enclosed in quotation marks. Quotation marks can be single or double quotes. For example: ' I like python ' = ' I like Python ' 2.1 String Functions
Name = ' Hand tech ' print (Name.title ()) >> Hand Tech
Method: Title () displays each word in the form of an initial letter size.
Print (Name.upper ()) >>hand Techprint (Name.lower ()) >> HAND Tech
Method: Upper () displays the string in size, lower () displays a string in lowercase to remove the function of the left and right string spaces: Lstrip (), Rstrip () 2.2 Merging (stitching) stringsPython merges two strings in a way similar to Java with a plus sign (+). For example:
Print (' This is my ' + ' first Python program! ') >> This is my first Python program!
When merging strings, note that variables that must be on both sides of the "+" must all be characters or string types, otherwise the error is. For example:
Print (' Hello ' + 2 + ' world ') Traceback (most recent call last):  File "<pyshell#14>", line 1, in <module>< C2/>print (' Hello ' + 2 + ' world ') typeerror:must is str, not int>>
Here you can convert this:
Print (' Hello ' + str (2) + ' world ')
>>hello 2 World
Convert "2" to a string type. 2.3 Tab\ t write a tab \ n line break \ r Enter for example:
>>> print ("\tpython")     Python >>> print ("Language:\n\tpython\n\tc\n\tjavascript") Language:    Python    C    JavaScript
2.4 removing whitespace
>>> favorite_language = ' python ' >>> favorite_language ' python ' >>> favorite_ Language.rstrip () ' Python ' >>> favorite_language ' python '

Method: Rstrip () Delete the right space of the string

Extension: Lstrip () Delete the left space of the string, strip () delete the space on both sides of the string 2.5 Correct use of quotation marks for stringsNote the reference to single quotation marks when using string quotes.
Message = ' One of Python ' s strengths is its diverse community. ' Print (message)
The above single quote is a common mistake. third, the number 3.1 integersTwo multiplication sign in Python represent the powers of the ship:
>>> 3 * * 29>>> 3 * * 327>>> 10 * * 61000000
3.2 floating pointIn Python, a number with a decimal point is called floating point number。 Note: Any subtraction of floating-point numbers are given floating-point numbers. 3.3 Conversion of numeric typesFor example:
Age = 23message = "Happy" + age + "Rd birthday!" Print (message)
This code will inevitably report an error after printing (Typeerror:can ' t convert ' int ' object to str implicitly) because the string type must be a string. To make an error, you must convert the number type to a string type by using a function like the cast, str ().
Mesasge = "Happy" + str (age) + "rd birthday!"
Iv. NotesIn Python, "#" is identified as a comment. Good comment is the ability that every good programmer must have.

Python Learning Notes-variables and simple data types

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.