Naming Variables

An introduction to the use of Variables in Python

Naming Variables

The naming of variables is quite flexible, but there are some rules you need to keep in mind:

  • Variable names must only be one word (as in no spaces)
  • Variable names must be made up of only letters, numbers, and underscore (_)
  • Variable names cannot begin with a number
Following the rules above, let’s look at both valid and invalid variable names:

Valid

Invalid

Why Invalid

my_int

my-int

Hyphens are not permitted

int4

4int

Cannot begin with a number

MY_INT

$MY_INT

Cannot use symbols other than _

another_int

another int

Cannot be more than one word

Something else to keep in mind when naming variables, is that they are case-sensitive, meaning that my_int, MY_INT, My_Int, and mY_iNt are all completely different variables. You should avoid using similar variable names within a program to ensure that both you and your current and future collaborators can keep your variables straight.

Introduction

Conventionally speaking, when naming variables it is customary to begin them with a lower-case letter and to use underscores when separating words. Beginning with an upper-case letter is not invalid, and some people may prefer camelCase or mixed upper- and lower-case letters when writing their variables, but these are less conventional choices.

Conventional Style

Unconventional Style

Why Unconventional

my_int

myInt

camelCase not conventional

int4

Int4

Upper-case first letter not conventional

my_first_string

myFirstString

camelCase not conventional

The most important style choice you can make is to be consistent. If you begin working on an existing project that has been using camelCase for its variable names, then it is best to continue using the existing style.

Styles

Write a program that stores a range of variables using the correct naming conventions.

Write a program that stores a range of variables using both correct and incorrect naming conventions.

Write a program that stores a range of variables using both correct and incorrect naming conventions. It should then output the names of the variables as well as the values and explain why the correct versions are used.

8.2 Naming Variables Task
Submit your completed task...
If you need to get in touch with Mr McG then this is the way….

What's your name?
image/svg+xml
Thank you [name], that's great.

Can I ask where you are from?
image/svg+xml
Hey [name], can I ask where you are from?
image/svg+xml
Hey [name], which class are you in?
image/svg+xml
OK [name], thanks for that.

I will need your email address so I can get back to you!!
image/svg+xml
Thanks [name], nearly there.

What can I help you with?
image/svg+xml

Thanks [name] from [whichclass][location], Mr McG will get your message…"
[message]"…and will get back to you ASAP via [email]. Catch you soon.

image/svg+xml
Thank you [name], Mr McG got your message.
Thanks [name], Mr McG will get back to you as soon as he can!!

Have a great day!!

K McGuinness - 2018

No personal data will be shared with any third party. The only data processed via this website is anonymous browsing data to best manage your user experience.