I still remember learning python for the first time. I thought it was such an intuitive language! I think it is faster to get started in python than any other programming languages. Today, I am writing a post to help those who want to learn to get a fast start.
Comments
So the first thing you should know is how to write a comment, this is ignored by the computer and is for people to read. A comment starts with a "#".
# ... now write your comment.
Math Expressions
If you want to use python like a calculator, this is very easy. Just write a math expression as you usually would:5 + 3
and python will return the result.
Of course, you can assign this expression to a variable:
x = 5 + 3and use this variable later.
For multiplication use * , and / is for division. So 2*3 is 6, and 6/2 is 3. There are other math functions like square-root that you can use. You can easily find them by googling.
Variables
Speaking of variables, to declare a variable in python is easy too. Suppose you want a variable with name "variablename" and you want it to have the string value "hello world", you would write:variablename = "hello world"We don't have to worry about the "type" of variablename, like is it an integer or a string, but if you intend the variable to be used for one thing, say it is an integer, you should be consistent else your code will be impossible to read!
If you want to create multiple variables at once, you can just say:
variablename1, ..., variablenamen = value1, ... , valuen
In other words, you list your variables, separating them by a comma, and then put an equal sign, and then list the values you want for the variables.
Function
Next, you will want to know how to write a function. In python, a function is defined using:
def myfunction (x, y):
# here write how your function use
# x and y...
# note how we have to tab everything
return x + y
So you start with the "def" keyword, then write your function name, then in brackets ( ), list the function arguments separated by a comma. Then, write a colon, and starting at the next line, write how your function works. Note how starting from the first line after "def myfunction (x, y):", everything is tabbed in once. In python, the tab defines the scope of your code.
After writing the function, you can use it by calling the function name with its arguments. For the example function above, we would write "myfunction(2,3)" to use it.
List
Python has a convenient data structure called a "list". It collects a bunch of things together, similar to an array. Suppose you want a list of numbers, this is created as:number_L = [1, 2, 3, 4]Later, you can index these numbers as you need, for example
number_L[0] gives 1.
Dictionary
A dictionary is like a list, except you don't index it with numbers, you can use strings, or anything with an order. These indices are called "keys". For example, we can have a dictionary with names and their id numbers:
dict = {'Joe': 123, 'Adam': 007}
We can then access the id numbers with the names. dict['joe'] will return 123. What python calls a dictionary, other languages call hash maps.
If statement
Next lets talk about the if statement. You may want to do one thing based on something being true, and another thing based on something else, otherwise a default action is done. In python, this is written as:
if onething:
# code for doing something
elif anotherthing:
# code for doing something
else:
# code for doing default action
"onething" and "anotherthing" are Boolean expression like "x < 5", which we can evaluate to True or False. "else" is a keyword that says the default code to execute if the if and all subsequent elif statements before it are all false. You can have as many elif as you need following an if, or you can have an if on its own with no elif and no else.
The while loop
There are two kinds of loops: thewhile loop and the for loop. The while loop terminates when a condition becomes false.
while condition:
# execute code
The above while loop will keep iterating until "condition" is false.
The for loop
Thefor loop has a predefined number of iterations. Often, a list is used to define how many times it iterates. For example, we can use the number_L list from before:
for i in number_L:
print i
This for loop would just print each element in the number_L in the order they are in the list. In a real for loop, you will do some processing to these list elements.
Classes
A class is a place where you can put a lot of functions and variables that are related to each other. For example, you can define a "car" class, and have variables like its current speed, and have a function called "speedup()" that increases speed, and a function called "slowdown()" that decreases speed. Such as class may look like:
class Car:Note that all functions in a class must have__init__(self, initialspeed): self.speed = initialspeed def speedup(self, add_speed): self.speed = self.speed + addspeed def slowdown(self, minusspeed): self.speed = self.speed - minusspeed
self as its first argument, and all class can optionally have an __init__ function where you explicitly initialize class variables. All class variables and functions need a self. when accessing them inside the class definition.
To create a car called mycar with initial speed 0, we would write:
mycar = Car(0)Then, if we wanted to speed up, we would write:
mycar.speedup(5)
Now you have the basics to start python programming! Good luck on your learning.
THE END!