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.
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 =10b =3# Arithmeticprint(a + b) # 13
13
print(a / b) # 3.333...
3.3333333333333335
# Comparisonprint(a > b) # True
True
print(a == b) # False
False
# Logicalx =Truey =Falseprint(x and y) # False
False
print(x or y) # True
True
1.2.2 R Code
a <-10b <-3# Arithmetica + b # 13
[1] 13
a / b # 3.333...
[1] 3.333333
# Comparisona > b # TRUE
[1] TRUE
a == b # FALSE
[1] FALSE
# Logicalx <-TRUEy <-FALSEx & 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: "))exceptEOFError: name ="TestUser" age =99print(f"Hello {name}, you are {age} years old.")
1.3.2 R Code
# Getting user input and displaying outputname <-readline(prompt ="Enter your name: ")age <-as.integer(readline(prompt ="Enter your age: "))cat("Hello", name, ", you are", age, "years old.\n")