Professional Documents
Culture Documents
- A layer of software logic between your code and the computer hardware on
your machine
- When Python package is installed on your machine, it generates a number of
components including at least:
1) an interpreter
2) a support library
- Python interpreter may take the form of an executable program, or a set of
libraries linked into another program
- Interpreter itself may be implemented as a C program, a set of Java classes,
or something else.
What do we need to run Python on our machines?
- Python interpreter on your computer
- Python installation details vary by platform:
1) Windows users fetch and run a self-installing executable file that puts
Python on their machines. Simply double-click and say Yes or Next at all
prompts.
2) Linux and Mac OS X users probably already have a usable Python
preinstalled on their computers—it’s a standard component on these
platforms today.
3) Other platforms have installation techniques relevant to those platforms.
For instance, Python is available on cell phones, game consoles, and
iPods, but installation details vary widely.
Resources to download
- You can download it from downloads page on the website,
https://www.python.org/. It may also be found through various other
distribution channels.
- Keep in mind that you should always check to see whether Python is already
present before installing it.
- On Unix and Linux, Python probably lives in your /usr directory tree. Because
installation details are so platform-specific, we’ll finesse the rest of this story
here.
- Google Colaboratory
- Jupyter notebooks
Program Execution
As Programmers:
- A Python program is just a text file containing Python statements.
- For example, the following file, named script0.py, is one of the simplest
Python scripts
- I could dream up, but it passes for a fully functional Python program:
print('hello world') # prints hello world
print(2 ** 100) # prints 2 to the power 100
Python’s View
- You can think of variables as a label for one or more pieces of information.
When doing analysis in python, all of your data (the variables you measured
in your study) will be stored as variables. You can also create variables for
other things too, which we will learn later on in the course.
- Examples:
Variable Value
Customer id 12347
Gender Male
- We'll begin with customer id. As a rule, all variables need to be single words without spaces, otherwise
python will throw a Syntax Error. Use underscores _ instead of spaces. It's best practice to also use
lowercase letter for variable names and to use names that are meaningful. For example, customer_id is
more descriptive then ci and uses an underscore instead of a space. Now that we have some basic rules
down, let's create the variable customer_id. We do this by using a special operator called the assignment
operator denoted by a single equal sign, =. The name of the variable is on the left side of the equals sign and
the value being assigned is on the right.
customer_id = 12345
customer_id
12345
Reserved Keywords
Avoid using the following reserved python words as variables names.
- Data types: categorize value in memory
e.g., int for integer, float for real number, str used for storing strings in
memory
- Numeric literal: number written in a program
No decimal point considered int, otherwise, considered float
Some operations behave differently depending on data type
- Can we convert from one data-type to another?
Built-in functions convert between data types
- int(item) converts item to an int
- float(item) converts item to a float
- Nested function call: general format: function1(function2(argument))
value returned by function2 is passed to function1
- Type conversion only works if item is valid numeric value, otherwise,
throws exception
Performing Calculations
•Math expression: performs calculation and gives a value
Math operator: tool for performing calculation
Operands: values surrounding operator
Variables can be used as operands
Resulting value typically assigned to variable
•Two types of division:
/ operator performs floating point division
// operator performs integer division
Positive results truncated, negative rounded away from zero
Operator Precedence and Grouping with Parentheses