Like every programming language worth its salt, Python allows you to assign values to variables, the fundamental building blocks of any programming languages. Think of a variable as a container which can be used to store data; this data is used in different places in your Python program. A variable can store both numeric and non-numeric data, and the contents of a variable can be altered during program execution. Finally, variables can be compared with each other, and you - the programmer - can write program code that performs specific actions on the basis of this comparison. The manner in which variables are assigned values should be clear from the following example: Although assigning values to a variable is extremely simple - as you've just seen - there are a few things that you should keep in mind here: * Every variable name must begin with a letter or underscore character (_), optionally followed by more letters or numbers - for example, "a", "data123", "i_am_god" * Case is important when referring to variables - in Python, a "cigar" is definitely not a "CIGAR"! * The equals (=) sign is used to assign a value to a variable. * It's always a good idea to give your variables names that make sense and are immediately recognizable - it's easy to tell what "net_profit" refers to, but not that easy to identify "np". * Unlike Java and C, Python does not require you to declare the type of variable prior to assigning it a value. It's behaviour here is closer to PHP, which allows you to assign any type of value to a variable without declaring it first. * Also like PHP, variables are created when they are assigned values - it is not necessary to declare them first. * Finally, Python variable names are not preceded with a $ sign, unlike most of its counterparts. Once you get used to it, you'll find that this actually adds to readability.
blog comments powered by Disqus |