Professional Documents
Culture Documents
2. Variables
a. Defining Variables
Named place for programmer to store data and later retrieve the data using the variable name
Remainder of variable name only can consist of letters, numbers and underscores:
Assessing Content variables are symbolic names for your container when you call a container,
you assess its contents
Reassignment
You can change the content of a variable in a later statement by simply reassigning a new value to it
c. Data Types
Type Meaning/Form Eg
Integer Number 40
Float Real numbers with decimal point dividing 40.0
the integer and fractional parts
String Sequence of Characters “40”
d. Conversion Function
Function Meaning
Int(x) Convert x into an integer
float(x) Convert x into a float
Str(x) Convert x into a string
Note: A variable of one type can be converted into another type, provided if data can be converted
results in type error
Example
fav_fruit[a:b] a= I start reading from here
b= I stop here (excluding this point)
Fav_fruit[a:b:c] c= steps
r.g. if you ask it to read
Note: Pythons starts with 0 + The conversion happens only if the data can be converted
b. String Concatenation
When you are adding certain things, they must be of the same type (e.g. string with string; integer with
integer)
c. Logic Errors: program is correct from syntax perspective, but output is obviously wrong
e.g.
d. Format Function
Limiting Decimal Places
Formatting Patterns
Formatting Percentages