Python · Visual Studio Code

Python – Quick start

In this article, we will step through how to get started with Python. How to install and get it up and running and look into some of the language syntax to get you started with your first Hello World application.

Install python from www.python.org
Go to https://www.jetbrains.com/pycharm and install PyCharm

Python with PyCharm
Run PyCharm and click “Create new project” and give it a name “test” and click Create

pythonQs1

On your Project, right click and New – File and give it a name “helloworld.py”
Right Click your helloworld.py file and click Run “helloworld”

pythonQs2

Python with Visual Studio Code

If you want to develop python with Visual Studio Code you will need to install an extension. Go to extensions in vs code and search for python and click install and reload vs code.

pythonQs3

Go to files tab and click “Open Folder” and create a folder “helloworld”.

pythonQs4

Click F1 or Ctrl+Shift+p and search for “Configure Task Runner” and chose “Others”

pythonQs5

pythonQs6

You will get a template file “tasks.json”.
We need to change some of the commands:

“command”: “python”
“args”: [“${file}”]

Now your tasks.json file should look like this:

{
    "version": "0.1.0",
    "command": "python",
    "isShellCommand": true,
    "args": ["${file}"],
    "showOutput": "always"
}

Create a new file “helloworld.py” and add print(“Hello World”)
Click “Ctrl+Shift+B” and you will get this result:

pythonQs7

Types:

Types: Python differ from a number of other languages in the way in handles types.  With Python you don’t have to declare a type. From one perspective that is bad thing since you will not get any warning up front if you for example try to multiply a number with a string. You can still use Type Hinting though to help you add datatypes, but this will still not prevent your application from running, even though you will get some help in your editor.

Let’s look at some built in types:

Floats and Integer

In C# you will have to declare the type first like:

Int width = 30
double height = 5.6

In Python you will do this:

width = 30
height = 5.6

And this will still work:

width + height = 35.6

You don’t have to worry about this, but if you have to you can cast an integer to a float with simply doing: float(width) == 30.0

Strings

In Python 3 its Unicode text by default.

On strings, you can use both single quotes or double quotes like: ‘This is a string’ == “This is a string”
It also supports a lot of utility methods like:

"This is a string".replace("a", "a replaced") == "This is a replaced string"
"this is a string".capitalize() == "This is a string"
"30".isdigit() == True
"This,is,a,string".split(",") == ["This", "is", "a", "string"]

stringformat = "string"
"This is a {0}".format(stringformat) = "This is a string"
f"This is a {stringformat}" = "This is a string"

Boolean

isTrue = true
int(isTrue) == 1

If Statements

width = 30
If width == 30:
   print(“The width is 30)
else:
   print(“The width is not 30)

isTrue = true

If isTrue:
   print(“It’s true”)

Opposite check:

if width != 30:.
If not isTrue:

Multiple checks:

If width == 30 and isTrue:
   print(“The width is 30 and it is true”)

if width == 0 or isTrue:
   print(“The width is not 30 but it is true”)

Lists

cars = [] – Empty list
cars = [“BMW”, “Volvo”, “Toyota”]

Access by index:

cars[1] == “Volvo”
cars[-1] == “Toyota” – Checking the last value

Add to list:

cars.append(“Tesla”)

Remove from list:

del cars[4]

Check for values:

“Volvo” in cars == True

Count:

len(cars) == 3

Loops

for item in cars:
print(item)

Break

for item in cars:
if item == “Vovlo”:
print(“Break when I found what I am looking for”)
break

Continue

for item in cars:
if item == “Volvo”:
continue
print(“All cars except Vovlo”)

Dictionaries

car = {“id”: 1, “name”:”BMW”}
cars = [
{“id”: 1, “name”:”BMW”},
{“id”: 2, “name”:”Volvo”},
{“id”: 3, “name”:”Toyota”}
]
car[“name”] == “BMW”

List keys in dictionary:

car.keys() == [“id”, “name”]

List values in a dictionary

car.values() = [“1”, “BMW”]

Change value in dictionary:

Car[“name”] = “Tesla”

Remove value from dictionary:

del car[“name”]

Exceptions

try:
      color = car[“color”]
except KeyError as error:
      print(error)
except Exception:
      print(“Some other error”)

Functions

cars = ["BMW", "Volvo", "Toyota"]

def get_cars(): 
    return cars

def print_cars():
    print("-------")
    for car in get_cars():
        print(car)

def add_car(name):
    cars.append(name)

def remove_car(index):
    del cars[index]

print_cars()

add_car("Tesla")

print_cars()

add_car(name="Ford")

print_cars()

remove_car(0)

print_cars()

name = input("Enter a car name:")

add_car(name)

print_cars()

Classes

cars = []

class Car:
    def __init__(self, name, id):
        self.name = name
        self.id = id
        cars.append(self)

    def __str__(self):
       return "Car " + self.name

bmw = Car("BMW", 1)
print(bmw)

Inheritance

cars = []

class Car:
    
    color = "Gray"

    def __init__(self, name, id):
        self.name = name
        self.id = id
        cars.append(self)

    def __str__(self):
       return "Car " + self.name


class SportsCar(Car):
    
    color = "Red"

car = SportsCar("Ferrari", 2)
print(car)

Modules

from classes import *

car = SportsCar("Ferrari", 2)
print(car)