Python For Dummies
This is the transcript of my seminar on a Crash Course for Python3, for AUVSI.
Why Python?
- It is very easy to learn and elegant to look at, even if your code is not perfect.
- No curly brackets or semicolon bull.
- It has the biggest programming community by far. This means anything you want to build is already available open source, and the numerous libraries allow you to make whatever you can dream of.
- For the above reasons, competent Python programmers are in great demand right now. So MONEEEYYYYY.
Will This Talk Get Me $100k?
Well, probably not. But after the next 45 minutes, you will be able to read and write Python quite well and maybe do some data juggling with numpy and pandas.
If you stick around until the end, I have something in store that will blow your mind.
The flow will tentatively go as follows,
- Intro to interpreter
- Hello World
- Variables And Types
- Operators
- Lists
- Loops
- Conditions (if else)
- Functions
- Imports
- Numpy Introduction
- Pandas Introduction
I’ve been told that there will be a separate session for numpy and pandas so we will not go too deep into that in this talk.
At the end of some sections, you will be given a problem to solve. We will not proceed until and unless everyone successfully solves it.
Intro To The Interpreter
When you open up IDLE or type python3 in your terminal, you should see something that looks like the following if your python is properly installed.
IDLE:
TERMINAL:
This is called the interpreter. Interpreters execute code line by line.
HELLO WORLD
Simply open up your interpreter and write
print("Hello World!")
And press enter. You should see it printed on the next line.
ezpz Lets move on.
VARIABLES AND TYPES
Python is called a statically typed language which means that you dont have to say beforehand what the data type a variable is. This makes it much more pleasing to look at.
Now you might ask, if data types dont exist how can I convert an integer to a decimal?
You would simply use data_type_you_want(variable_name)
syntax. This is called typecasting.
OPERATORS
The usual addition (+), subtraction (-), multiplication (*) and division (/) are obviously present.
But apart from that we have the modulo (%) operator, used to find the remainder after division of two numbers.
And the power(**) operator, to raise to a power.
LISTS
Lists
are very similar to arrays. They can contain any type of variable, and they can contain as many variables as you wish. Lists
can also be iterated over in a very simple manner. Here is an example of how to build a list.
List elements can be accessed using indexes with square []
brackets . Lists are indexed from 0 .
To add an element to the end of a list use the append
function.
To remove an element from a list,
to remove by value use remove
,
to remove by index use del
,
LOOPS
NOTE:
Python3 does not have curly brackets to enclose units of code. It makes use of blocks.
A block is a area of code of written in the format of:
block_head:
1st block line
2nd block line
...
Loops can run any number of commands as many times until a specified condition is valid.
In Python, there are two types of loops, for and while.
for loops iterate over a sequence,
and while loops repeat as long as a certain boolean condition is valid.
Conditions
if-else statements are the pillars of programming. Its quite simple. if a condition checks out then we run some code but if not then we run something else.
We can also add more conditions using and
and or
operators between them. Open up your editor, lets write some conditions.
FUNCTIONS
What are Functions?
Functions are a convenient way to divide your code into useful blocks, allowing us to order our code, make it more readable, reuse it and save some time. Also functions are a key way to define interfaces so programmers can share their code.
How do you write functions in Python?
Functions in python are defined using the block keyword “def”, followed with the function’s name as the block’s name. For example:
def my_function():
print("Hello From My Function!")
Functions may also receive arguments (variables passed from the caller to the function). For example:
def my_function_with_args(username, greeting):
print(f"Hello, {username} , From My Function!, I wish you {greeting}")
Functions may return a value to the caller, using the keyword- ‘return’ . For example:
def sum_two_numbers(a, b):
return a + b
To call a function that has been defined, simply use function_name(arguments)
,
my_function_with_args("AUVSI","Happy Holidays")
IMPORTS
Functions that are written in the same folder in different script or are installed through pip, can be imported directly into your program using from module_name import function_name
.
from random import random
print(random())
That gives your script access to the random function which prints a random number between 0 and 1.
NOW FOR THE SURPRISE
I was really excited to talk about this on the sesh but sadly we went way overtime :(
So lets say I made a script for a function that loops over a list like so,
Now, remove the colon at the start of the block and add an end
at the end of each block and save this new script with a .rb
extension. Now let me run this with the ruby
interpreter.
Congratulations, you now not only know python, but also ruby! (which is if you recall from the top, the language with the most pay!)