#!/usr/bin/env python
# Programs normally execute one line after the other from top to bottom.
# But sometimes we need to make a decision while the program is running.
# Some of these decisions will send the program down one path or the other.
# To do this in Python we use the if-elif-else statements.
# The first thing to note is Python uses indentation to define code blocks
# rather than the {} notation found in C or Java. This means in Python
# indentation of code blocks is manditory. We also don't actually
# type "then" as in "if condition is true then do somemthing". Instead we use
# a colon ":". Because why not?
# Basic if statement.
A = 0 # Integer.
if A == 0: print A
# A more complex example with indentation defining a code block.
A = [1,2,3,4,5] # List of integers.
if A[0] != 0:
print ("A at index 0 is equal to %d.") % A[0]
print ("A at index 0 is equal to %d.") % A[1]
print ("A at index 0 is equal to %d.") % A[2]
print ("A at index 0 is equal to %d.") % A[3]
print ("A at index 0 is equal to %d.") % A[4]
# Both the examples above will only print the value of A if the condition is
# met. But often we need alternative actions to be taken when the condition
# has not been met. For this we add the else statement.
A = 1
if A == 0:
print A
else:
print ("A is not 0.")
# By adding additional if statements we can test for multiple possibilities.
# We do this in Python by using elif.
A = 2
if A == 0:
print A
elif A == 1:
print A
elif A == 2:
print A
elif A == 3:
print A
else:
print ("The value of A does not match the test.")
Showing posts with label integer. Show all posts
Showing posts with label integer. Show all posts
Monday, 27 July 2015
Variables and Data Types
#!/usr/bin/env python
# Python Variables and Data Types
# The first thing to know about a variable is that it is just a place in the
# computer's memory where data is held. Think of them as boxes.
# Variables have names to make them easier to work with. In most programming
# languages variable names can be made up of numbers and letters. And they
# normally must start with a letter or underscore character "_". Python in this
# respect is like any other programming language. A variable name is called an
# "identifier". Identifiers are case sensitive. Python keywords cannot be used
# as identifiers.
# Python variables are loosely typed. The interpreter assumes the variable type
# based on the content of the variable. Variables can be integers, long integers
# octal literals (base 8), hexadecimal literals (base 16), floating-point
# numbers and complex numbers.
# Python also supports some complex data structures. These are strings, lists,
# tupels, dictionaries and sets. A list is like an array in Java or C. Except a
# list in Python can consist of mixed types. Tupels, dictionaries and sets are
# similar to lists. But they have different uses, attributes and methods of
# access.
# Examples of numbers.
a = 1 # Integer.
b = 42000000000000000000L # Long integer.
c = 010 # Octal literal.
d = 0xA1F # Hexadecimal literal.
e = 0XA1F # Hexadecimal literal.
f = 45.33 # Floating-point number.
g = 9.5541e-10 # Floating-point number.
h = 4 + 4j # Complex number.
print a
print b
print c
print d
print e
print f
print g
print h
# Examples of strings.
i = "Hello World" # String with double quotes "
j = 'Hello World' # String with single quotes '
k = '''Hello World can extend over multiple lines
and contain 'single quotes' and "double quotes"
which is crazy.''' # String with triple quotes '''
l = '\"Hello World\"' # String with single quotes, printing double quotes.
m = "\'Hello World\'" # String with double quotes, printing single quotes.
print i
print j
print k
print l
print m
# Example of a list.
n = ["H","e","l","l","0"] # A simple list.
o = [["W","o","r","l","d"],2,3.45,0xA1F] # A list composed of multiple types.
print n
print o
print n[0]
print n[1]
print n[2]
print n[3]
print n[4]
print o[0][0]
print o[0][1]
print o[0][2]
print o[0][3]
print o[0][4]
print o[1]
print o[2]
print o[3]
# Example of a tuple.
p = ("Tuples","are","immutable","lists")
print p
print p[0]
print p[1]
print p[2]
print p[3]
# Example of a dictionary.
q = {"Name" : "Bob","Age" : 20}
print q
print q["Name"]
print q["Age"]
# Example of a set.
r = set(("Hello World",i))
s = {"Hello World",i}
print r
print s
# Why doesn't "Hello World" print twice for each set as we would expect?
# Sets contain non-repeating data only. Since "Hello World" and "i" are the same,
# one is discarded.
# Python Variables and Data Types
# The first thing to know about a variable is that it is just a place in the
# computer's memory where data is held. Think of them as boxes.
# Variables have names to make them easier to work with. In most programming
# languages variable names can be made up of numbers and letters. And they
# normally must start with a letter or underscore character "_". Python in this
# respect is like any other programming language. A variable name is called an
# "identifier". Identifiers are case sensitive. Python keywords cannot be used
# as identifiers.
# Python variables are loosely typed. The interpreter assumes the variable type
# based on the content of the variable. Variables can be integers, long integers
# octal literals (base 8), hexadecimal literals (base 16), floating-point
# numbers and complex numbers.
# Python also supports some complex data structures. These are strings, lists,
# tupels, dictionaries and sets. A list is like an array in Java or C. Except a
# list in Python can consist of mixed types. Tupels, dictionaries and sets are
# similar to lists. But they have different uses, attributes and methods of
# access.
# Examples of numbers.
a = 1 # Integer.
b = 42000000000000000000L # Long integer.
c = 010 # Octal literal.
d = 0xA1F # Hexadecimal literal.
e = 0XA1F # Hexadecimal literal.
f = 45.33 # Floating-point number.
g = 9.5541e-10 # Floating-point number.
h = 4 + 4j # Complex number.
print a
print b
print c
print d
print e
print f
print g
print h
# Examples of strings.
i = "Hello World" # String with double quotes "
j = 'Hello World' # String with single quotes '
k = '''Hello World can extend over multiple lines
and contain 'single quotes' and "double quotes"
which is crazy.''' # String with triple quotes '''
l = '\"Hello World\"' # String with single quotes, printing double quotes.
m = "\'Hello World\'" # String with double quotes, printing single quotes.
print i
print j
print k
print l
print m
# Example of a list.
n = ["H","e","l","l","0"] # A simple list.
o = [["W","o","r","l","d"],2,3.45,0xA1F] # A list composed of multiple types.
print n
print o
print n[0]
print n[1]
print n[2]
print n[3]
print n[4]
print o[0][0]
print o[0][1]
print o[0][2]
print o[0][3]
print o[0][4]
print o[1]
print o[2]
print o[3]
# Example of a tuple.
p = ("Tuples","are","immutable","lists")
print p
print p[0]
print p[1]
print p[2]
print p[3]
# Example of a dictionary.
q = {"Name" : "Bob","Age" : 20}
print q
print q["Name"]
print q["Age"]
# Example of a set.
r = set(("Hello World",i))
s = {"Hello World",i}
print r
print s
# Why doesn't "Hello World" print twice for each set as we would expect?
# Sets contain non-repeating data only. Since "Hello World" and "i" are the same,
# one is discarded.
Labels:
dictionary,
floating-point,
hexadecimal,
integer,
list,
lists,
long integer,
octal,
Python,
sets,
string,
tuple
Location:
Scotland, UK
Subscribe to:
Posts (Atom)