top of page

CEP 824_ Unit 2 Variables

  • Writer: Shuaiqi Hu
    Shuaiqi Hu
  • Jan 24, 2022
  • 1 min read

Explore Variables

Variables store information that's used and referenced throughout your code.
  • Additional properties: scope, type, access, lifetime, and storage requirements.



Every variable in Python is a reference to an object.

This means that all variables store memory locations where the various pieces of data can be found. This is why Python variables do not have to be declared with a specific data type.

extra careful in managing data in the code


Syntactic Requirements

variable names cannot

  1. contain spaces; for example, Birth Date is not a valid variable name; instead, use birthDate or birth_date;

  2. start with a number; they need to start with a letter; for example, 6Keys is not a valid variable;

  3. contain a mathematical operator or certain punctuation characters; the underscore character is generally allowed;

  4. be a “reserved word” or “keyword” in the language (such as for or while).

Naming Conventions

Some of these conventions include:

  1. using camel case to distinguish separate words in a variable name; the first word is in lower case and subsequence words are capitalized, like the humps in a camel (e.g., birthDate);

  2. using underscores to separate words in a variable name (e.g., birth_date);

  3. using all upper case for constants (e.g., INITIALIZE_SIZE or MAX_AGE).



Practice

Go ahead and try it out using Scratch or repl.it! Take a few minutes and use this CS Unplugged activity to display numbers by using a variable. You'll find an example, hints, and the solution under the "Languages" section on the right of the page.




repl.it! activity : Link


for number_of_dots in range(5):
    print (2 ** number_of_dots)


Comentarios


bottom of page