Uploaded by s7813690

3Chapter 2 Variables

advertisement
Chapter 2: Variables and Operators
A. What are variables?
Variables are nothing but reserved memory locations to store values. This means that when you create a
variable you reserve some space in memory.
Python is completely object oriented, and not "statically typed". You do not need to declare variables
before using them, or declare their type. Every variable in Python is an
Example of variable types:
object.
B. Assigning Values to Variables
Python variables do not need explicit declaration to reserve memory
space. The declaration happens automatically when you assign a value to
a variable. The equal sign (=) is used to assign values to variables.
The operand to the left of the = operator is the name of the variable and
the operand to the right of the = operator is the value stored in the
variable.
x = 123
# integer
x = 123L
# long integer
x = 3.14
# double float
x = "hello"
# string
x = [0,1,2]
# list
x = (0,1,2)
# tuple
x = open (‘hello.py’, ‘r’) # file
There are certain rules or naming conventions for naming variables. The following are the rules:
▪
▪
▪
▪
▪
Reserved key words such as if, else, and so on cannot be used for naming variables
Variable names can begin with _, $, or a letter
Variable names can be in lower case and uppercase
Variable names cannot start with a number
White space characters are not allowed in the naming of a variable
You can assign values to the variable using = or assignment operator.
Syntax:
<variable name>= < expression >
Example:
An integer is defined as variable x and given (assigned) the value 7.
A float is defined as variable y and assigned a value 95.3.
A String is defined as myString1 and myString2 and assigned a value ‘hello’. Note that Strings can be
defined by either single quotes or double quotes.
Multiple assignments can be done on more than one variable as well.
Simple operators can be executed on numbers and strings:
Issuing multiple commands in one line can be used as well.
B.1 Using Comments and Todo Comments
Comments can also be used along with white space to document your code.
Notice that for single line comment the hashtag symbol # (pound key) is used, while for multi-line
comments triple double quotes “ are used.
If you are not able to finish your work and you want to give yourself a reminder, a to do list, you can
easily do this with a comment and the todo command.
Note that on line 14, a comment was inserted with a todo task, line 17 has one as well. Doing this will
allow you to track the things you want to continue working on in another time.
You can see all your todo list on the TODO Tab:
B.2 Using Escape Sequences
Python also provides escape sequences or shortcuts to commands for printing.
Which prints:
Here is the list of all available escape sequences in Python.
B.3 Refactor and Rename
What if you wanted to rename a variable name you have used several times?
You have three options to do that:
1. Manually find the variable name and retype.
2. Use find and replace.
3. Use Refactor and rename – this is by far the safest way to rename your variable names.
Using Refactor and Rename:
▪
▪
Right Click on your variable name and select Refactor and Rename
Type in the new variable name you want to use
C. Python Data Types
Data types are the classification or categorization of data items. Data types represent a kind of value
which determines what operations can be performed on that data.
Numeric, non-numeric and Boolean (true/false) data are the most used data types.
Python has the following standard or built-in data types:
1. Numeric
A numeric value is any representation of data which has a numeric value. Python identifies three
types of numbers:
Integer: Positive or negative whole numbers (without a fractional part)
Float: Any real number with a floating point representation in which a fractional component is
denoted by a decimal symbol or scientific notation
Complex number: A number with a real and imaginary component represented as x+yj. x and y
are floats and j is -1(square root of -1 called an imaginary number)
2. Boolean
Data with one of two built-in values True or False. Notice that 'T' and 'F' are capital. true and
false are not valid Booleans and Python will throw an error for them.
3. Sequence Type
A sequence is an ordered collection of similar or different data types. Python has the following
built-in sequence data types:
String: A string value is a collection of one or more characters put in single, double or triple
quotes.
List: A list object is an ordered collection of one or more data items, not necessarily of the same
type, put in square brackets.
Tuple: A Tuple object is an ordered collection of one or more data items, not necessarily of the
same type, put in parentheses.
4. Dictionary
A dictionary object is an unordered collection of data in a key:value pair form. A collection of
such pairs is enclosed in curly brackets. For example: {1:"Roger", 2:"Mark", 3:"Susan", 4: "Mat"}
Download