Login Register






Thread Rating:
  • 0 Vote(s) - 0 Average


Python Tutorial Chapter 2 - More Python filter_list
Author
Message
Python Tutorial Chapter 2 - More Python #1
Chapter -- > 2 :~# More python

#2.1 --|Numbers|--

Python is just like powerful language such as C and java, supports 3 data types for scientific calculations. The three DATA TYPES are :

1. Integer
2. String
3. Boolean


+================================================================+

#2.2 --|Variables|--

In python language, Variables needn't declared.
You can call them right from where you need them!

For instance
Variable can be assigned by just simply typing x = 6
But in other lanugages such as C++
You have to declare variable as follows;

Quote:int x;
Another example
Quote:>>>x = 3
>>>y = 3
>>>print(x*y)
9

First variable x is assgined integer 3
Then variable y is assgined integer 3
After that, print function is used to output the results.
Now you should have grub the basics of variable Biggrin

+===============================================================+

2.3# --|Functions|-- (General)

Now we're going to talk about functions briefly
Functions are vital in every programming lanugages.
You can define your own function or use the existing function.
Function is sort of like a feature, for example "pow(x,y)" is a function.
Functions can help make your program more powerful and as well as making your program easier to maintain!

In C++ when writing functions (I lost memory on C++ not very sure Biggrin)
Quote:1. Definition must be first declared
2. Write the definition
3. Call the Defininition
are bound to be followed.

+==================================================================+

2.4# --|Modules|-- (General)

You may have realized the import statement above some of the programs.
Eg. import math
Import is a command that import the modules into the program.
Modules can make program more powerful.
We'll come to modules later...

+==================================================================+


2.5# --|Comments|--

If you have tried any one of programming language, you would have heard about comment definately. Every programming language has it's own style of writting comments.
For example, For eg. C style comment is /* comment */
In python you just need to use sharp sign "#"

Code:
#!usr/bin/env python
#This is a comment
Print('hello')
#This is a comment again


Interpreter will not execute the string/code after # sign, it knows that it's a comment, and simply skips. Commenting makes the program more readable and easier to debug.
We'll go into details about writing functions in next chapter.

Commonly used Functions
1.print()
2.input() / raw_input()
2.float
3.complex

Example, 1, 2, 3, 4 are integers.
Python do not have long integer type as C language. It's included in integer type.
This might be a weakness.

6.39 and 787.6E-9 is float data type.
787.6E-9 = 787.6*10-9

It also supports complex number that are commonly used in Mathematics.
Example 9+7j

Now we'll deal with number operatoin. #OpNo Biggrin
Open up Python shell. Let's get started.

Code:
>>> 1+1
2
>>>1/2
0
Now why 1 divided by 2 didn't result in 0.5?
Because Python took it as integer. But it's not the problem anymore in python version 3.

Code:
>>>1.0/2.0
0.5

In this way, we can calcualte basic Arithmetic Operations, such as addition, subtruction, multiplication, and division.
“+ - / *”
Another one
Code:
>>>1%2

"%" operator is used to calculate remainder of a division.

Code:
>>>3**2
9
"**" means power

Code:
>>>-3**2
-9
>>>(-3)**2
9
Latest version didn't have the buffer overflow anymore!
Hexadecimal and Octal are also supported

Code:
>>>0xCD
205

Hex form is 0xXX
>>>010
8
This is ocatal, but in version 3 it changed to following

Code:
>>>0o10
8

Now, we've reached the end of Chapter 2
Smile

Thanks for reading.
Ref : Multiple Sources
MrGeek @ HC :-> logout

Reply





Messages In This Thread
Python Tutorial Chapter 2 - More Python - by MrGeek - 05-17-2013, 12:42 PM



Users browsing this thread: 1 Guest(s)