Skip to content

Getting Started

Using python shell

  • Open terminal (ctrl+alt+t).
  • Type:

$ python
- If you have python 3 then type:
$ python3
This will open python shell in your terminal.

>>>

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")
- In case of python 2 the above syntax will also work along with the following syntax :
$ python
>>> print "Unlock the world of security" 
The output will be displayed in the python shell as:
>>> 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'>
Conversion to a valid datatype is possible with the help of typecasting.

>>>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
Index value starts from 0.

home

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
It starts from 0 to 24, thus in total 25 characters.

>>>a[0:24]

Output

'I can crack your password'
For only the first 5 characters.
>>>a[0:5]

Output

'I can'
For the 10th character.
>>>a[10]

Output

'k'
To Extract the characters at even indices.

To include the last character, we need to specify the ending point as 25.

>>>a[0:25:2]

Output

'Icncakyu asod'
Python has built-in function 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(" ")
It will give us a list with the substrings which are separated by what is specified in quotations (here split(" ") i.e. space).

Output

['I', 'can', 'crack', 'your', 'password']
Example 2

>>>new='safe, secure, security'
>>>new.split(",")

Output

['safe', ' secure', ' security']