1  Basic Programming

Basic programming involves learning how to instruct computers using a language they can interpret. It focuses on understanding how data is represented, how logic is constructed, and how interaction occurs through input and output.

1.1 Variables and Data Types

A variable is a symbolic name that refers to stored information. Variables can hold various types of data, and choosing the right data type is crucial for efficient and correct operations.

Common Data Types:

Type Description Example
Integer Whole numbers 1, 25, -100
Float Decimal numbers 3.14, -0.001
String Text "Hello", 'World'
Boolean Logical values (True or False) True, False

1.1.1 Python Code

# Variable assignment
name = "Alice"           # string
age = 28                 # integer
height = 1.68            # float
is_active = True         # boolean

# Print variable types
print(type(name))        # <class 'str'>
<class 'str'>
print(type(age))         # <class 'int'>
<class 'int'>
print(type(height))      # <class 'float'>
<class 'float'>
print(type(is_active))   # <class 'bool'>
<class 'bool'>

1.1.2 R Code

# Variable assignment
name <- "Alice"          # character
age <- 28                # integer (numeric)
height <- 1.68           # float (numeric)
is_active <- TRUE        # boolean (logical)

# Check variable types
typeof(name)             # "character"
[1] "character"
typeof(age)              # "double"
[1] "double"
typeof(height)           # "double"
[1] "double"
typeof(is_active)        # "logical"
[1] "logical"

1.2 Operators

Operators are symbols used to perform operations on variables and values. They are essential for calculations, comparisons, and logical decisions in programming.

Common Operator Types:

Operator Type Python Example R Example Use Case
Arithmetic +, -, *, / +, -, *, / Mathematical operations
Comparison ==, !=, >, < ==, !=, >, < Comparing values
Logical and, or, not &, |, ! Combining logical expressions
Assignment =, +=, -= <-, ->, = Assigning values

1.2.1 Python Code

a = 10
b = 3

# Arithmetic
print(a + b)   # 13
13
print(a / b)   # 3.333...
3.3333333333333335
# Comparison
print(a > b)   # True
True
print(a == b)  # False
False
# Logical
x = True
y = False
print(x and y)  # False
False
print(x or y)   # True
True

1.2.2 R Code

a <- 10
b <- 3

# Arithmetic
a + b    # 13
[1] 13
a / b    # 3.333...
[1] 3.333333
# Comparison
a > b    # TRUE
[1] TRUE
a == b   # FALSE
[1] FALSE
# Logical
x <- TRUE
y <- FALSE
x & y    # FALSE
[1] FALSE
x | y    # TRUE
[1] TRUE

1.3 Input and Output

  • Input: Receives data from users or external sources.
  • Output: Displays information to the screen or interface.

1.3.1 Python Code

input() may cause an error when rendering this document. Use placeholder values if rendering non-interactively.

try:
    name = input("Enter your name: ")
    age = int(input("Enter your age: "))
except EOFError:
    name = "TestUser"
    age = 99

print(f"Hello {name}, you are {age} years old.")

1.3.2 R Code

# Getting user input and displaying output
name <- readline(prompt = "Enter your name: ")
age <- as.integer(readline(prompt = "Enter your age: "))
cat("Hello", name, ", you are", age, "years old.\n")