Python Lesson 10 notes (local variables and global variables)

Source: Internet
Author: User
Speaking of global variables and local variables today, the foreign teacher said that global variables are like stars in Hollywood and everyone knows. Local variables must be declared before they can be used. Otherwise, people do not know.

# The function, Jennifer is local
Def God ();
Global Jennifer
Jennifer = 'pretty'

# Main program, always global, Hollywood becomes famous, and everybody knows
God ()
Print Jennifer

The output result is: Pretty

# The function, Jennifer is local
Def God ();
Global Jennifer
Jennifer = 'pretty'
# Main program, always global, Hollywood becomes famous, and everybody knows
Jennifer = 'beautiful'
God ()

The output result is: Beautiful

Do you know why ?? Because Jennifer is a global variable, all subsequent local variables are invalid... Only the preceding ones are recognized.

By the way, the teacher also said the types of various variables .....
>>> Number = 5
> FL = 4.3
>>> Sentence = 'you have smelly boots'
>>> B = false
>>> Type (number)
<Type 'int'>
>>> Type (FL)
<Type 'float'>
>>> Type (sentence)
<Type 'str'>

But I have a question?
>>> 2.34
2.3399999999999999
How can 2.34 be 2.3399999999999999 ??
Humbly ask for advice from uncles, sisters, and uncles.

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.