Python programming (basics)

A computer program is a sequence of instructions that can be interpreted and executed by computer. It is written in a programming language that can be interepreted by computer. Python is a high-level programming language, which can be easily interpreted by human. Python interpreter converts the Python code (high-level) into instructions (low-level) the computer can understand.

Python is composed of objects

For better human readability and easy usage, Python uses objects that have various functionality in it. For example, a text or a list of characters is represented as a string object, and it provides functions that allows users to extract a part of it or to concatenate two string objects.

# Create a string object with name of 'sample_text' 
simple_text = "hello"

# Get sub-string 
simple_text[2:5]

# Concatenate two strings 
simple_text + simple_text 

Flow of information

Programs are often written to execute computations, which is a series of process including (1) getting input, (2) storing data, (3) calculating or transforming the input into other form, (4) returning the result. It can be understood as a process of converting one form of data into another forms. For example, a table of data with the time and the atmosphere temperature can be converted into a figure showing that the temperature increases as the sun rises and decreases as the sun sets. The program converts table format data into figure format data, to provides visual insights on the data.

Therefore, an usual program will have the following sets of instructures.

  1. Get inputs
  2. Store the input into variables (objects)
  3. Convert variables into other types of variables
  4. (a series of further data conversions)
  5. Return the final variable

These instructions can be written in a series of command lines. Yet, in many cases we need to control the flow of information. A certain operation should be done when a certain condition is met. Operations can be repeated for a number of times. In Python if, for, while sentences can be used to control the flow of information.

1. Data types

Basic types of data is defined as various types of objects in Python. Numbers can be represened by int, float, complex, and texts by string. A list and tuple works as a container, while the former can be changed while the later cannot be changed. A set represents mathematical sets, where each item is unique. A dictionary is composed of keys and values, where values are retrieved by the key.

2. Flow control

The instructions in a prgram is executed one line after the other line. Yet, the flow can be controlled by using if, for, and while sentences. A condition and an instruction block, marked by indentation, is defined in these sentences. The block of an if sentence is executed only when the condition is met. The block below for and while are repeated while the condition is met.

3. Simple tutorial

3.1. Variable

A variable can be thought as a name of an object. A piece of data (numbers, text, etc) is stored as an object (int, float, str). You can site it by assigning a name to it, the name is called variable. More precisely, a variable is a designator of the object.

A variable is assigned by using an operator ‘=’.

name = "name is a variable designating this string object"

3.2. Print values

You can print the value (content) of a variable.

print("hello")
print("you", "can", "write something here")
print("numbers can also be printed:", 1234)

3.3. Data type for numbers

An integer and real number is represented by an int and float obejct. The object can be implicitly called by writing number it self, like 10 and 10.0 as shown below. If the type is not explicitly given, the interpreter infers the type of data.

# int (정수), float (실수)
x_i = 10 
x_f = 10.0
y_i = int(4)
y_f = float(4)
print(x_i, x_f, y_i, y_f)
print(type(x_i), type(x_f), type(y_i), type(y_f))

3.4. Data type for text

A text or string of characters is represented by an str object. String object can be created by enclosing text with or marker. The object has multiple functions, which allows you operations like retrieving part of the sentence.

# define a text data 
msg = "hello! good to see you!"

# get length of the text
print(len(msg))

# get first 5 characters
print(msg[:5])

3.5. Container data type

Multiple items can be stored in a list and tuple object. The former change changed, while the later is not. Unique items are stored in a set object. A dict object stores key and value pairs.

# List 
fruits = ["apple", "orange", "grape"]

# tuple 
flowers = ("rose", "lily", "lilac")

# retrieval of data 
for fr in fruits:
    print(fr)

# modification of data (addition of a fruit)
fruits.append("watermelon")

# set 
fruits_set = {"apple", "orange", "grape", "grape"}

# dict
telephone_number = {"James": "010-xxx-xxxx", "Mary": "010-yyy-yyyy"}
print(telephone_number["James"])

3.6. Flow control

The flow of data can be controlled by using if, for, and while sentence.

# Print fruit with name containing letter 'o'.
for fr in fruits:
    if 'o' in fr:
        print(fr)

# print 1 to 10 using while sentence
cnt = 1
while cnt <= 10:
    print(cnt)
    cnt += 1

Resources

Basic grammer of an programming language can be learned from various online sources. W3Schools is a good online tutorials to learn Python. It covers a wide variety of issues including File Handling, Modules, Matplotlibs, etc. It is advised to visit each of the tutorial.

  1. Learn basics by reading this post
  2. Participate in online tutorials (W3School): https://www.w3schools.com/python/
  3. Practice to write your own code

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top