Python Basics #1 [ n00b Friendly]

Rox

Right off the assembly line
[size=medium][align=center]Beginner's Python
-Rox
[/align][/size]



If you dont know programming, It is the way in which we tell computer to do something, step by step. Each step is called a command.
For example, If we are to make computer drink a tea, the steps are getup from table, go to table and take tea. We should not say, Take tea, it is in table. It is ok for friends to understand but computer doesnt.


About Python
==


  • [*]Python is cross platform, meaning it can be run on windows, linux, mac, everything.
    [*]Python saves files in .py extension.
    [*]Python is relatively simple.
    [*]Python is low-performance language[Is slow to run, unlike C++,C# etc.]
    [*]Python is a high level language[More closer to humans than machines]

Downloading Compiler
==

Download from here and install it.
Now open Python GUI[IDLE] from Start Menu --> Python.

So that is a 'Compiler' and that is where we type in our programming 'commands'

Hello World
==

If you have programming experience before, unlike other languages like C++ or VB, we dont need header files or other things.
For simply outputting 'Hello World', we simple type:
Code:
"Hello World"
in the Python GUI And Press Enter. It will print 'Hello World'.
If you have Quotes in the program itself, ad a \[backslash] before it.



Mathematics in Python
==

Maths Functions work on python simply like you are writing it on a calculator.
For eg, Typing 2+2 and pressing Enter gives 4.
Typing 2*2 and pressing Enter gives 4.
Typing 2/2 gives 1.
However, if u type 3/2, it also gives 1 with no decimal. To have decimal in your program , you have to give a decimal to either numberator(3) or denominator(2).
eg:) 3.0/2 = 1.5

Compiling - It is how we convert our 'English-like' language into numbers that computer can understand(0s and 1s)

If you want the remainder of something, for example, if you want the remainder of 3/2, you use the '%' symbol between 3 and 2.
ie, 3%2 gives the remainder of 3/2 which is 1.

We call '%' modulus.
Now, if you want the power of a number , you use two stars(*) .
For example if you want the power of 2 raised to 5, you write 2**5 and it gives you 2^5 which is 32.

All the above rules applies to -(ve) numbers also.


Variables
======

Variables are letters or symbols which act as temporary storage values for numbers.
For example, x is a variable. apple is a variable. Only if we assign x and apple numerical values like
x=5, apples = 1000 etc.
So how do we assign values in python?

Unlike other languages, you dont need to 'declare' a variable first.
Just typing
x = 5
in the promt assigns 'x' the value of 5.
now, if we type x + 10 and press enter, we get 15, since value of x is 5 and hence is x+5.
We can also assign multiple variables like x = 5, apples = 85 and then if we do
x + apples
we get 90.

Now these type of declarations at the time of programing is called 'Compile time initialization' mostly used term in C++. So we, the programmers declare the variable at the time of writing code.
Now, if we are going to code something like a calculator, it should be the user that types the values. For that, we use 'input' function,

for example, typing
Code:
f = input("Enter Number: ")
and pressing enter displays
Enter Number: _
And we enter a number. That value is assigned to 'f'.
So that is all about variables in python. Now try making a calculator.

For that we need multiple line commands. Go to File-->New Window. And type the codes there.
in there,if we type "hello world", its not gonna display "Hello WOrld". for that, we need to type
print "Hello World".

Then type the variables and other things. Also, to run the program, go to 'Run' in the new window and Press 'Run Module'. Press ok if it asks to save.

So Hope you learnt something. Thanks for reading.
 
Top Bottom