Creating variables

Introduction to Data Science in Python

Hillary Green-Lerman

Lead Data Scientist, Looker

Filing a missing puppy report

missing-puppy-report

name = "Bayes"
height = 24
weight = 75.5
Introduction to Data Science in Python

Rules for variable names

  • Must start with a letter (usually lowercase)
  • After first letter, can use letters/numbers/underscores
  • No spaces or special characters
  • Case sensitive (my_var is different from MY_VAR)
# Valid Variables
bayes_weight
b
bayes42
# Invalid Variables
bayes-height
bayes!
42bayes
Introduction to Data Science in Python

Error messages

bayes-height = 3
File "<stdin>", line 1
    bayes-height = 3
                   ^
SyntaxError: can't assign to operator
Introduction to Data Science in Python

Floats and strings

  • float: represents an integer or decimal number

    height = 24
    weight = 75.5
    
  • string: represents text; can contain letters, numbers, spaces, and special characters

    name = 'Bayes'
    breed = "Golden Retriever"
    
Introduction to Data Science in Python

Common string mistakes

  • Without quotes, you'll get a name error.
owner = DataCamp
File "<stdin>", line 1, in <module>
    owner = DataCamp
NameError: name 'DataCamp' is not defined
  • If you use different quotation marks, you'll get a syntax error.
fur_color = "blonde'
File "<stdin>", line 1
    fur_color = "blonde'
                       ^
SyntaxError: EOL while scanning string literal
Introduction to Data Science in Python

Displaying variables

name = "Bayes"
height = 24
weight = 75

print(height)
24
Introduction to Data Science in Python

Let's practice!

Introduction to Data Science in Python

Preparing Video For Download...