What makes variables easy to use in Python is that you do not have to declare the type. The programmer keeps track of the variable type. For example, you can code num=9 and later num='Bob,' and Python doesn't care. In addition, variables do not need to be declared before use; you simply think of a name and start using the variable. This makes Python a dynamically typed language.
In Python, you cannot declare a variable as a constant. Instead, variables hold constants, and we typically use all caps to denote that a certain variable is a constant, though it can be changed later in our code, just like any variable. If you want a variable to stay constant, just don't change it.
Note that in Python, strings can be defined with either single or double quotes, whereas other languages are much more picky about this syntax.
Yet another interesting Python feature allows you to declare multiple variables in a single line:
In addition to the usual arithmetic operations, if you import the math module, you can use Python's built-in functions (which include mathematic constants like pi, e, and tau).
Sources:
General Variable Use
Very Good Source on Variables and Data Types in Python
Another Great Source on Variables
Powers and Roots
Is Python statically typed, or dynamically typed? When languages allow you to declare variables without specifying their type (called "type inference") they are usually dynamically typed. Does Python allow this?
ReplyDeleteDr. Palmer, Python is dynamically typed! I added further information about this into the post.
Delete