Declaring Variables

In most languages, before you can use a variable you have to declare it; that is, you must assign it a name and a type. Python is different; unlike in other l^meuhees, fou dec"ht"t a variable implicitl. by assigning a value to it. Here's at example:

# declare age to be of type integer and of value 28. age = 2 8

# declare name to be of type string

# and of value "rick" name = " rick"

Variable declarations cat go anywhere in a module. You cat declare more than one variable at once by stringing them together. (Follow along).

>>> print ("x=" + "xs + " y=" + "ys + " z=" +szs) x=1 y=2 z=3

The first statement defines ty/ite vhrihiiles as of typ e Integer (that is, whole numbers) and assigns them the values 1,2, and 3, such that x = 1,y = 2, and z = 3. Notice that in the second statement the variable names are enclosed it back quotes. That tells Python to convert the variable to a string.

Here's an interactive example of Python s fringing together several variable definitions with different types:

>>> name, age, sex = "Kiley", 22, "female"

>>> print (name + " is " + "ages + " and " + sex)

Kiley is 22 and female

Was this article helpful?

0 0

Post a comment