Getting Started with Python: A Beginner's Guide
Tags: python, datascience, beginners
Published: 2024-06-30 02:00:17

Welcome to your journey into Python programming! Python is a versatile and powerful programming language that is easy to learn and fun to use. In this guide, we will cover the basics of Python, including how to write your first line of code, understand data types, work with variables, and define functions. Let's dive in!

## First Line of Code

Every journey begins with a single step. In Python, the first step is often writing a simple line of code to print a message to the screen.

```python
print('Hello My Lovely!')
```

This code uses the `print` function to display the text `'Hello My Lovely!'` on the screen.

## Data Types

Understanding data types is crucial in programming. Here are the basic data types in Python:

- **int**: Represents integers (e.g., 1, 2, 3)
- **float**: Represents floating-point numbers (e.g., 1.5, 2.75)
- **str**: Represents strings, which are sequences of characters (e.g., 'hello', 'Python')
- **boolean**: Represents Boolean values, either `True` or `False`

Example usage:

```python
print(1 + 2)      # Output: 3
print(1 + 2.5)    # Output: 3.5
print(1 + '2')    # Type Error: can't add str with int
```

## Variables

Variables are names that we give to certain values in our code. They allow us to store and manipulate data.

```python
a = 2 + 3
# a here is a variable holding the value 5
```

## Functions

Functions are useful for writing reusable code. A function is a block of code that performs a specific task.

```python
def luasSegitga(alas, tinggi):
    return alas * tinggi / 2
```

In this example, `luasSegitga` is a function that calculates the area of a triangle.

## Comparison Operators

Python comparison operators return Boolean results: **True** or **False**.

| Symbol | Name                    | Expression | Description                 |
|--------|-------------------------|------------|-----------------------------|
| ==     | Equality operator       | a == b     | a is equal to b             |
| !=     | Not equal to operator   | a != b     | a is not equal to b         |
| >      | Greater than operator   | a > b      | a is larger than b          |
| >=     | Greater than or equal to| a >= b     | a is larger than or equal to b |
| <      | Less than operator      | a < b      | a is smaller than b         |
| <=     | Less than or equal to   | a <= b     | a is smaller than or equal to b |

### Comparison Operators With Strings

Python uses Unicode values to compare strings:

| Expression | Description |
|------------|-------------|
| "a" == "a" | If string "a" is identical to string "a", returns True. Else, returns False |
| "a" != "b" | If string "a" is not identical to string "b" |
| "a" > "b"  | If string "a" has a larger Unicode value than string "b" |
| "a" >= "b" | If the Unicode value for string "a" is greater than or equal to the Unicode value of string "b" |
| "a" < "b"  | If string "a" has a smaller Unicode value than string "b" |
| "a" <= "b" | If the Unicode value for string "a" is smaller than or equal to the Unicode value of string "b" |

## Logical Operators

Logical operators are used to combine multiple Boolean expressions.

| Operator | Description                        |
|----------|------------------------------------|
| and      | Returns True if both expressions are True |
| or       | Returns True if at least one expression is True |
| not      | Returns True if the expression is False |

Example:

```python
x = True
y = False
print(x and y)  # Output: False
print(x or y)   # Output: True
print(not x)    # Output: False
```

## Data Structures

Python provides several data structures to store collections of data.

### Lists

Lists are ordered collections of items.

```python
my_list = [1, 2, 3, 'a', 'b', 'c']
```

### Tuples

Tuples are similar to lists but are immutable.

```python
my_tuple = (1, 2, 3, 'a', 'b', 'c')
```

### Sets

Sets are unordered collections of unique items.

```python
my_set = {1, 2, 3, 'a', 'b', 'c'}
```

### Dictionaries

Dictionaries store key-value pairs.

```python
my_dict = {'name': 'John', 'age': 30}
```

## Conclusion

This guide covered the basics of Python, including writing your first line of code, understanding data types, working with variables, defining functions, and using comparison and logical operators. With this foundation, you are well on your way to becoming proficient in Python programming. Keep practicing, and happy coding!