Distinguishing the operator from the equals sign is important and can be a

Distinguishing the operator from the equals sign is

This preview shows page 1 - 2 out of 2 pages.

sign from mathematics, it has a different meaning in Python. Distinguishing the = operator from the equals sign is important, and can be a source of frustration for beginner programmers. Just remember, whenever you see the = operator, whatever is to the right of it is being assigned to a variable on the left. Variable names are case-sensitive, so a variable named phrase is distinct from a variable named Phrase (note the capital P). For instance, the following code produces a NameError: >>> phrase = "Hello, world" >>> print(Phrase) Traceback (most recent call last): File "", line 1, in NameError: name 'Phrase' is not defined When you run into trouble with the code examples in this book, be sure to double-check that every character in your code—including spaces—exactly matches the examples. Computers can’t use common sense to interpret what you meant to say, so being almost correct won’t get a computer to do the right thing! Rules for Valid Variable Names Variable names can be as long or as short as you like, but there are a couple of rules that you must follow. Variable names can only contain uppercase and lowercase letters (A–Z, a–z), digits (0–9), and underscores (_). However, variable names cannot begin with a digit. For example, phrase, string1, _a1p4a, and list_of_names are all valid variable names, but 9lives is not. 52 3.3. Create a Variable Note Python variable names can contain many different valid Unicode characters. Unicode is a standard for digitally representing text used in most of the world’s writing systems. That means variable names can contain letters from nonEnglish alphabets, such as decorated letters like é and ü, and even Chinese, Japanese, and Arabic symbols. However, not every system can display decorated characters, so it is a good idea to avoid them if your code is going to be shared with people in many different regions. You can learn more about Unicode on Wikipedia. Python’s support for Unicode is covered in the official Python documentation. Just because a variable name is valid doesn’t necessarily mean that it is a good name. Choosing a good name for a variable can be
Image of page 1
surprisingly difficult. However, there are some guidelines that you can follow to help you choose better names.
Image of page 2

You've reached the end of your free preview.

Want to read both pages?

  • Summer '16
  • nushawan
  • Equals sign, Imperative programming, relational operator

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes