Basics
Basics
Hello students and welcome to the notes section for this course.Rather than
having huge sums of text which would be quite boring to read. I have taken
care to keep the notes short, simple and up to the point. I have ignored the
non-useful things and included important Python facts.
What is Python:
Python is a widely used high-level, general-purpose, interpreted,dynamic
programming language Python is founded by Guido van Rossum is a Dutch
programmer who is best known as the author of the Python programming
language.
You can use the general-purpose Python console to write Python code or
you can use any IDE, which stands for Integrated Development
Environment. PyCharm is the IDE which we have used in the course but you
can use any IDE which supports Python.
For example
var1 = 'Hello World!'
var2 = "Python Programming"
The input of the user will be interpreted. If the user e.g. puts in an integer
value, the input function returns this integer value. If the user on the other
hand inputs a list,
the function will return a list.
If the input function is called, the program flow will be stopped until the
user has given an input and has ended the input with the return key. The
text of the optional parameter, i.e. the prompt, will be printed on the
screen.
The input of the user will be returned as a string without any changes.If this
raw input has to be transformed into another data type needed by the
algorithm, we can use casting function.
Variables in Python
Variables are nothing but reserved memory locations to store values. This means that
when you create a variable you reserve some space in memory.
Based on the data type of a variable, the interpreter allocates memory and
decides what can be stored in the reserved memory. Therefore, by
assigning different data types to variables, you can store integers, decimals
or characters in these 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. Python has five standard data types −
• Numbers
• String
• List
• Tuple
• Dictionary
• If statements in Python:
•
Elif statement in Python:
• Sometimes there are more than two conditions and we need more
than two branches. In such cases we use the elif statement.elif is an
abbreviation of else if. Again, exactly one branch will be executed.
There is no limit of the number of elif statements but only a single
(and optional) final else statement is allowed and it must be the last
branch in the statement:
•
if choice == 'a':
print("You chose 'a'.")
elif choice == 'b':
print("You chose 'b'.")
elif choice == 'c':
print("You chose 'c'.")
else:
print("Invalid choice.")
Lists in Python:
Python has six built-in types of sequences, but the most common
ones are lists and tuples.
• There are certain things you can do with all sequence types. These
operations include indexing, slicing, adding, multiplying, and
checking for membership. In addition, Python has built-in functions
for finding the length of a sequence and for finding its largest and
smallest elements. The list is a most versatile datatype available in
Python which can be written as a list of comma-separated values
(items) between square brackets. Important thing about a list is that
items in a list need not be of the same type.
Creating a list is as simple as putting different comma-separated
values between square brackets
• For example
• listone = ['physics', 'chemistry', 1997, 2000];
•
• List functions in Python
•
append() : to append an item to the list.
to append to a list named fruits, type in fruits.append("Banana")
• function to calculate the length of the list:
print(len((fruits))
This line of code will print out the length of the list named fruits
• Insert function:
This function allows you to insert some item to the list, this function is
similar to append but
it allows you to insert item at a particular position.
example:
fruits.insert(1,"banana")
This line of code places the item banana at position 1 in the list fruits.
• The index function.
it returns the index value / position of particular item in the list.
Example:
print(fruits.index("Peach"))
This line of code will return the index position of item Peach.
• Range function in Python
The built-in range function in Python is very useful to generate
sequences of
numbers in the form of a list.
• The given end point is never part of the generated list;
• range(10) generates a list of 10 values, the legal indices for items of a
sequence of length 10.
• It is possible to let the range start at another number, or to specify a
different increment (even negative;
• Sometimes this is called the ‘step’):
• example:
• >>> range(1,10)
[1, 2, 3, 4, 5, 6, 7, 8, 9]
• # You can use range() wherever you would use a list.
• a = range(1, 10)
for i in a:
print i