Getting Started¶
Using python shell¶
- Open terminal (ctrl+alt+t).
- Type:
$ python
$ python3
>>>
Lets tryout a simple python code to print "Unlock the world of security", all you need is to use print function.
- open terminal and type:
$ python3
>>> print("Unlock the world of security")
$ python
>>> print "Unlock the world of security"
>>> Unlock the world of security
Basic Data Types¶
- Integer eg: -4, 0, 32
- Float eg: -44.3, 0.55, 7.0
- Complex eg: 3+2i, 4-i, -2-3i
- Boolean true or false
- String eg: 'Smile', '44', 'disc0ver'
To check the datatype, python has built in function type().
>>> n=1729
>>>type(n)
<class 'int'>
>>>s="H4cker"
>>>type(s)
<class 'str'>
>>>f=17.29
>>>type(f)
<class 'float'>
>>>f=212.34
>>>int(f)
212
>>>num=33
>>>float(num)
33.0
Strings¶
These are set of characters closed with double or single quotes.
string="security"
Here security is written in double quotes, hence is taken as a string.
With length as 8.
To check length, python has built-in function, len(string name)
.
>>> string="security"
>>>len(string)
Press enter.
Output
8
Things to know:
Syntax for extracting a part of the string :
string_name[starting:ending:incrementation]
>>>a='I can crack your password'
>>>len(a)
Output
25
>>>a[0:24]
Output
'I can crack your password'
>>>a[0:5]
Output
'I can'
>>>a[10]
Output
'k'
To include the last character, we need to specify the ending point as 25.
>>>a[0:25:2]
Output
'Icncakyu asod'
string_name.split("")
.
It will split the string into substrings when it encounters the character in quotations. If nothing is specified in the quotes, then it will display error.
Example 1
If we want to separate the string into substrings, when there is a space:
>>>a.split(" ")
Output
['I', 'can', 'crack', 'your', 'password']
>>>new='safe, secure, security'
>>>new.split(",")
Output
['safe', ' secure', ' security']