---
# System prepended metadata

title: Basics

---


# Python Basics

How to install python3
```bash
sudo apt-get install python3
```

How to check version
```bash
devasc@labvm:~$ python3 -V
Python 3.8.2
```

How to start python3
```bash
devasc@labvm~$ python3
Python 3.8.2 (default, Mar 13 2020, 10:14:16) 
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
```

Calculation
```bash
devasc@labvm~$ python3
Python 3.8.2 (default, Mar 13 2020, 10:14:16) 
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 2+3
5
>>> 10-4
6
>>> 2*4
8
>>> 20/5
4.0
>>> 3**2
```

How to print string?
```bash
devasc@labvm~$ python3
Python 3.8.2 (default, Mar 13 2020, 10:14:16) 
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> "Hello World!"
'Hello World!'
>>> 'Hello World!'
'Hello World!'
>>> print("Hello World!")
Hello World!
```

Quit python
```bash
devasc@labvm~$ python3
Python 3.8.2 (default, Mar 13 2020, 10:14:16) 
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> quit()
devasc@labvm:~$
```

# Script

0. Turn on VSCode
1. Create a file with extension `.py`
2. Execute :)

> Executed directly on VSCode using Run > Run Without Debugging
```bash
devasc@labvm:~/labs/devnet-src/python$  env DEBUGPY_LAUNCHER_PORT=36095 /usr/bin/python3 /home/devasc/.vscode/extensions/ms-python.python-2020.4.76186/pythonFiles/lib/python/debugpy/no_wheels/debugpy/launcher /home/devasc/labs/devnet-src/python/hello-world.py 
Hello World!
```

> Executed manually on command prompt (same directory/folder)

```bash
devasc@labvm:~/labs/devnet-src/python$ python3 hello-world.py 
Hello World!
devasc@labvm:~/labs/devnet-src/python$
```

> Executed manually on command prompt (different dir)

```bash
devasc@labvm:~$ python3 ~/labs/devnet-src/python/hello-world.py 
Hello World!
devasc@labvm:~$
```

# Data Type
```bash
Python 3.12.6 (tags/v3.12.6:a4a2d2b, Sep  6 2024, 20:11:23) [MSC v.1940 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> type("DevNet")
<class 'str'>
>>> type(False) # important depan caps
<class 'bool'>
>>> type(22)
<class 'int'>
>>> type(22.11)
<class 'float'>
>>>
```

# Boolean

```bash
>>> 1<2
True
>>> 1<1
False
>>> 1==1
True
>>> 1>=1
True
>>> 1<=1
True
```

# String Concatenation

```bash
>>> str1 = "Aan"
>>> str2 = "Was"
>>> str3 = "Here"
>>> print(str1 + " " + str2 +  " " + str3)
Aan Was Here
>>> print(str1, str2, str3)
Aan Was Here
```

# Type casting (change data type)

Cannot directly + int to a string
```bash
>>> x=5
>>> print("The value of x is " + x)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can only concatenate str (not "int") to str
```

So change the data type of x into string first
```bash
>>> print("The value of x is " + str(x))
The value of x is 5
```

Careful wit the location it is assigned

```bash
>>> x=5 # initially it is int, therefore output pon int
>>> print("The value of x is " + x)
>>> type(x)
<class 'int'>

```

Reassigned again, but as a str
```bash
>>> x = str(5)
>>> type(x)
<class 'str'>
```

Formatting output
```bash
>>> num = 39/7
>>> print(f"The value of num is {num}")
The value of num is 5.571428571428571
>>> shortnum = "{:.2f}".format(num)
>>> f"The value of 39/7 is {shortnum}."
5.57
```

# List

```bash
>>> hostnames=["M1","N1","N2","R1","R2","R3"]
>>> type(hostnames)
<class 'list'>
>>> len(hostnames)
6
>>> hostnames
['M1','N1','N2','R1','R2','R3']
>>> hostnames[0]
'M1'
>>> hostnames[-1]
'R3'
>>> hostnames[0] = "R1"
>>> hostnames
['R1','N1','N2','R1','R2','R3']
>>> del hostnames[3]
>>> hostnames
['R1','N1','N2','R2','R3']
```

# Dictionary

```bash
>>> csProgram ={"R":"Computer Network & Security", "B":"Bioinformatics", "P":"Data Engineering"}
>>> type(csProgram)
<class 'dict'>
>>> csProgram
{'R': 'Computer Network & Security', 'B': 'Bioinformatics', 'P': 'Data Engineering'}
>>> csProgram['R']
'Computer Network & Security'
>>> csProgram["J"]="Software Engineering"
>>> csProgram
{'R': 'Computer Network & Security', 'B': 'Bioinformatics', 'P': 'Data Engineering', 'J': 'Software Engineering'}
>>> csProgram["R"]=["Networks & Security", "Cyber Security"]
{'R': ['Computer Network & Security', 'Cyber Security'], 'B': 'Bioinformatics', 'P': 'Data Engineering', 'J': 'Software Engineering'}
```

# Getting an input from a function

```bash
>>> fullName = input("What is your full name? ")
What is your full name? Megat
>>> print(fullName)
Megat
```

# if-else

```python
if marks == 100:
    print("Good Job :D")
else:
    print("fool")
```

# if-elif-else
```python
if marks == 100:
    print("Good Job!")
elif marks == 90:
    print("Acceptable")
else:
    print("fool")
```

# for loop
```python
devices=["R1", "R2", "R3", "S1", " S2"]
for i in devices:
    print(i)
```

# for wit condition
```python
for item in devices:
    if "R" in item:
        print(item)

R1
R2
R3
```

# append into list

```python
item = ['RE1', 'ME1']
wtf = []
for i in item:
    if "RE" in i:
        wtf.append(i)

print(wtf)
RE1
```

# while
```python
x=1
y=10
while(x < y):
    print(x)
    x = x + 1
```

# break
```python
x=1
y=10
while True:
    print(x)
    x = x + 1
    if x > y:
        break
```

# file access

content of file is thiz
```bash
devasc@labvm:~/$ cat devices.txt
Cisco 881 Router
Cisco 888 Router
Cisco 1100 Router
Cisco 4321 Router
Cisco 4331 Router
Cisco 4351 Router
Cisco 2960 Catalyst Switch
Cisco 3850 Catalyst Switch
Cisco 7700 Nexus Switch
Cisco Meraki MS220-8 Cloud Managed Switch
Cisco Meraki MX64W Security Appliance
Cisco Meraki MX84 Security Appliance
Cisco Meraki MC74 VoIP Phone
Cisco 3860 Catalyst Switch
```
```python
file=open("devices.txt" , "r")
for item in file:
    print(item)
file.close()
```

```bash
Cisco 881 Router

Cisco 888 Router

Cisco 1100 Router

Cisco 4321 Router

Cisco 4331 Router

Cisco 4351 Router

Cisco 2960 Catalyst Switch

Cisco 3850 Catalyst Switch

Cisco 7700 Nexus Switch

Cisco Meraki MS220-8 Cloud Managed Switch

Cisco Meraki MX64W Security Appliance

Cisco Meraki MX84 Security Appliance

Cisco Meraki MC74 VoIP Phone

Cisco 3860 Catalyst Switch
```
# file access with strip

```python
wtf = []
file=open("devices.txt" , "r")
for item in file:
    meow = item.strip()
    wtf.append(meow)
    print(item)
file.close()
```

```bash
Cisco 881 Router
Cisco 888 Router
Cisco 1100 Router
Cisco 4321 Router
Cisco 4331 Router
Cisco 4351 Router
Cisco 2960 Catalyst Switch
Cisco 3850 Catalyst Switch
Cisco 7700 Nexus Switch
Cisco Meraki MS220-8 Cloud Managed Switch
Cisco Meraki MX64W Security Appliance
Cisco Meraki MX84 Security Appliance
Cisco Meraki MC74 VoIP Phone
Cisco 3860 Catalyst Switch
```

