Python basics
Python basics
If you find that you do not have Python installed on your computer, then you
can download it for free from the following website: https://www.python.org/
Python Quickstart
Python is an interpreted programming language, this means that as a developer you write
Python (.py) files in a text editor and then put those files into the python interpreter to be
executed.
(if you don’t have a text editor download Notepad++)
The way to run a python file is like this on the command line:
C:\Users\Your Name>python helloworld.py
Or by creating a python file on the server, using the .py file extension, and
running it in the Command Line:
if 5 > 2:
if 5 > 2:
x = 5
y = "Hello, World!"
Comments
Python has commenting capability for the purpose of in-code documentation.
Comments start with a #, and Python will render the rest of the line as a comment:
#This is a comment.
print("Hello, World!" )
A comment does not have to be text that explains the code, it can also be
used to prevent Python from executing code:
Multiline Comments
add a multiline string (triple quotes) in your code, and place your
comment inside it:
"""
This is a comment
written in
"""
print("Hello, World!")
Python Va riables
Python has no command for declaring a variable.
Example 1
x = 5
y = "Harry"
print(x)
print(y)
Variables do not need to be declared with any particular type, and can even change type after they have been set.
Example 2
x = 4 # x is of type int
x = "Sally" # x is now of type str
print(x)
Variable Names
A variable can have a short name (like x and y) or a more descriptive name (age, carname, total_volume). Rules
for Python variables:
● int
● float
● complex
Variables of numeric types are created when you assign a value to them:
Python Operators
Operators are used to perform operations on variables and values.
In the example below, we use the + operator to add together two values:
Python Type
Int
Int, or integer, is a whole number, positive or negative, without decimals, of unlimited
length.
Example
x = 1
y = 35656222554887711
z = -3255522
print(type(x))
print(type(y))
print(type(z))
Float can also be scientific numbers with an "e" to indicate the power of 10.
x = 35e3
y = 12E4
z = -87.7e100
print(type(x))
print(type(y))
print(type(z))
Complex numbers are written with a "j" as the imaginary part:
x = 3+5j
y = 5j
z = -5j
print(type(x))
print(type(y))
print(type(z))
Type Conversion
You can convert from one type to another with the int(), float(), and complex() methods:
x = 1 # int
y = 2.8 # float
z = 1j # complex
a = float(x)
b = int(y)
#convert from int to complex:
c = complex(x)
print(a)
print(b)
print(c)
print(type(a))
print(type(b))
print(type(c))
Python Strings
Strings in python are surrounded by either single quotation marks, or
double quotation marks.
Example:
print("Hello")
print('Hello')
Assign String to a Variable
Assigning a string to a variable is done with the variable name followed by an equal sign and the string:
a = "Hello"
print(a)
Python Booleans
Booleans represent one of two values: True or False.
print(10 > 9)
print(10 == 9)
print(10 < 9)
Example
Print a message based on whether the condition is True or False:
a = 200
b = 33
if b > a:
else:
Example
String, int and boolean data types:
list2 = [1, 5, 7, 9, 3]
print(type(mylist))
print(thislist)
Access Items
List items are indexed and you can access them by referring to the index
number:
Example:
Print the second item of the list:
thislist = ["apple", "banana", "cherry"]
print(thislist[1])
Negative Indexing
-1 refers to the last item, -2 refers to the second last item etc.
Example
Print the last item of the list:
print(thislist[-1])
Range of Indexes
You can specify a range of indexes by specifying where to start and where to end
the range.
When specifying a range, the return value will be a new list with the specified
items.
Example
Return the third, fourth, and fifth item:
thislist = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"]
print(thislist[2:5])
Change Item Value
Example
thislist[1] = "blackcurrant"
print(thislist)
Append Items
To add an item to the end of the list, use the append() method:
thislist.append( "orange")
print(thislist)
Insert Items
To insert a list item at a specified index, use the insert() method.
Example
thislist.insert(1, "orange")
print(thislist)
Extend List
To append elements from another list to the current list, use the extend() method.
Example
thislist.extend(tropical)
print(thislist)
Remove Specific Item
The remove() method removes the specified item.
Example:
Remove "banana":
thislist.remove( "banana")
print(thislist)
Remove Specified Index
Example
thislist.pop(1)
print(thislist)
Strings are Arrays
Like many other popular programming languages, strings in Python are arrays of bytes representing
unicode characters.
However, Python does not have a character data type, a single character is simply a string with a
length of 1.
Example
Get the character at position 1 (remember that the first character has the position
0):
a = "Hello, World!"
print(a[1])
Looping Through a String
Since strings are arrays, we can loop through the characters in a string, with a for loop.
Example
Loop through the letters in the word "banana":
for x in "banana":
print(x)