Basics of Python
- write many lines of code to a file using a text editor and
then use the Python interpreter to run the entire file
- write and run
each line of code, one after the other using the Python console
While the console enables us to quickly
evaluate short pieces of code, the text editor enables us to save our code as a
text file that we can run over and over again
Run the following line of code in the
console (notice the omitted
print()
statement)
and see what happens:
Piece of code: 1288+639
Python, and many programming languages, uses
the order of operations rules from mathematics to determine
the specific priority that expressions have. Here's the ordering:
- Parentheses
- Exponent
- Multiplication or Division
- Addition
or Subtraction
An
easy way to remember the order of operations is PEMDAS.
The equals sign (
=
) is called the assignment operator because it's
used to assign the value on the right to the variable name on the left.
a=2
When naming the variables, there are some rules
- · Variable names cannot contain special characters.
- · Variable names cannot start with numbers.
- · Variable names cannot be one of the Reserved words in Python.
When you assign a value to a variable, no
output is displayed.
Try assigning a value to a variable name in the console and see what's displayed.
Try assigning a value to a variable name in the console and see what's displayed.
To display the value associated with a variable, we need to use the
print()
statement
or type the name of the variable and press Enter:
The two most common numerical types in
Python are integer and float.
The most common non-numerical type is a string.
To represent a piece of text as a string
value, surround the text with either single quotes (
'
) or double quotes ("
).
Unlike variable names, strings can contain
special characters and spaces.
We can look up the data type of a variable's value using the
type()
function. List in python can be used to store multiple values.
To create an empty list, assign a pair of
empty brackets
[]
to a variable:
To add values to a list object, use the
list.append()
method.
You can organize your code by inserting comments.
These are the two main types of comments you can
add to your code:
- inline comment
- single-line comment
How
to access and work with the values in a list we've created
Fruits = [‘banana’, ‘apple’, ‘orange’]
index
|
0
|
1
|
2
|
values
|
banana
|
apple
|
orange
|
This is called Zero based Indexing.
You can access banana using the following
code:
Last index= len(Fruit)
– 1
Lists have a feature called slicing that allows you to return all of the values between a
starting index and an ending index.
To open a file in Python, we use the
open()
function.
This function accepts two different arguments (inputs) in the parentheses.- the name of the file (as a string)
- the mode of working with the file (as a string)
Eg:
To open a file named
story.txt
in read mode
open("story.txt",
"r")
We can assign the File object to a
variable so we can refer to it later:
a
= open("story.txt", "r")
Here
a is an object which act as an interface to the file and the methods contained.
We use the
read()
function to
read the contents of story.txt
.
a
= open("story.txt", "r")
b=a.read()
print(b)
In Python, we can use the
split()
method to
turn a string object into a list of strings
Eg: Open the
crime_rates.csv file in read mode and read the content. Content should be split
on the delimiter ‘\n’ and slice the values from 0 to 5.Then write
a for loop that splits each element in rows on
the comma delimiter, and appends the resulting list to a
new list named final_data.
Then, use the print() function
and list slicing to display the first five elements in final_data.
We can actually convert between types using
specific functions in Python:
If we try to convert a string value that
isn't 100% an integer value just represented as a string, we'll get an error:
Comments
Post a Comment