summaryrefslogtreecommitdiff
path: root/basic-python.txt
blob: 5fa2a8d56da7c2f9e6f8a8bc08fda23b8f464c8f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
*Script


*Hello and welcome to this tutorial on Basic Python using Python.

This tutorial formally introduces Python as a language . Through this tutorial we will be able to understand Basic Data types like number , Boolean and strings .Some basic operators , simple input/output and basic conditional flow . 

In numbers Python supports three kinds of data types ,

floats,integers and complex numbers

An integer can be defined as follows :
a=13

This make a an integer variable with value 13 .

You can also type 9 around 20 times 

a=99999999999999999999999 . as you can see Python does not have a limit on how long an integer has to be . Isn't that great . 

Now will try a float.

let's type 
p=3.141592  if you type out p now you will notice that it is not absolutely equal to p you typed in . The reason for that is how a computer saves decimal values . 

Apart from integer and float, Python has an in-built support for complex numbers. Now we try to assign a complex value to a variable .
Type:
c = 3+4j
As you can see ,the notation for complex numbers is similar to the one used in electric engineering. 
We will now try some operations on complex numbers . First we will try to get the absolute value of the complex number . For this we will use the abs built in function . For this do :
abs in parenthesis c . 

Do get the imaginary part of c you can do :

c.imag

and similarly for real part do :

c.real

Python also has Boolean as a built-in type .

Try it out just type ..
 t=True , note that T in true is capitalized .    
  
You can apply different Boolean operations on t now for example :


f=not t , this saves the value of not t that is False in f. 

We can apply other operators like or and and ,

f or t gives us the value True while 
f and t gives us the value false.

You can use parenthesis for precedence , 

Lets write some piece of code to check this out .

a=False
b=True
c=True

To check how precedence changes with parenthesis . We will try two expressions and their evaluation.

do
(a and b) or c 
 
This expression gives the value True

where as the expression a and (b or c) gives the value False .

Now we will have a look at strings 

type 
w="hello"

w is now a string variable with the value "hello"

printing out w[0] + w[2] + w[-1] gives hlo if you notice the expression for accessing characters of a string is similar to lists . 

Also functions like len work with strings just like the way they did with lists

Now lets try changing a character in the string in the same way we change lists .

type :
w[0]='Capital H'  

oops this gives us a Type Error . Why? Because string are immutable . You can change a string simply by assigning a new element to it . This and some other features specific to string processing make string a different kind of data structure than lists .