Python language variables 2 (naming conventions, type conversions)

Source: Internet
Author: User

1. Naming rules

1. The starting bit is the letter (case) or underline ('_')

2. Other parts are letter (case), underscore ('_') or number (0-9)

3. Case sensitive

2. Experience First :

#Ask the user their namename = input ("") # make the first letter ofTheir name upper case#an D The rest of the name lower case# and thenprint itprint (name[0].upper () + name[1:].lower ()) /c10> 

Where # indicates a comment.

Note that when you enter a name, you need to use quotation marks to indicate that you are entering a string, otherwise idle will think you are typing an unnamed variable, error (name ' * * * ' is not defined)

3. Type conversion

Code similar to part 2nd:

num = input ("Enter A number:")answer = num*3print("" + answer)

When you enter 1, are you waiting for the program to output a 3 for you?

The result is an error (Cannot concatenate ' str ' and ' int ' objects) telling you that you cannot connect a string "Your ..." and an integer answer.

At this point we need the type conversion to tell idle that we need to print answer like a string.

1 num = input ("")2 answer = num*33print(  "" + str (answer)) 

You might be wondering about line 2nd, why don't we tell Idle,num is a numeric variable, and it can handle num as a numeric variable? In this regard, poke the usual functions of the Python language, and follow the explanations of input (), Raw_input ().

Python language variables 2 (naming conventions, type conversions)

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.