DEV Community

Cover image for Getting Started with Python: A Beginner's Guide
tinApyp
tinApyp

Posted on

Getting Started with Python: A Beginner's Guide

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.

print('Hello My Lovely!')
Enter fullscreen mode Exit fullscreen mode

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:

print(1 + 2)      # Output: 3
print(1 + 2.5)    # Output: 3.5
print(1 + '2')    # Type Error: can't add str with int
Enter fullscreen mode Exit fullscreen mode

Variables

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

a = 2 + 3
# a here is a variable holding the value 5
Enter fullscreen mode Exit fullscreen mode

Functions

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

def luasSegitga(alas, tinggi):
    return alas * tinggi / 2
Enter fullscreen mode Exit fullscreen mode

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:

x = True
y = False
print(x and y)  # Output: False
print(x or y)   # Output: True
print(not x)    # Output: False
Enter fullscreen mode Exit fullscreen mode

Data Structures

Python provides several data structures to store collections of data.

Lists

Lists are ordered collections of items.

my_list = [1, 2, 3, 'a', 'b', 'c']
Enter fullscreen mode Exit fullscreen mode

Tuples

Tuples are similar to lists but are immutable.

my_tuple = (1, 2, 3, 'a', 'b', 'c')
Enter fullscreen mode Exit fullscreen mode

Sets

Sets are unordered collections of unique items.

my_set = {1, 2, 3, 'a', 'b', 'c'}
Enter fullscreen mode Exit fullscreen mode

Dictionaries

Dictionaries store key-value pairs.

my_dict = {'name': 'John', 'age': 30}
Enter fullscreen mode Exit fullscreen mode

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!

Top comments (1)

Collapse
 
raajaryan profile image
Deepak Kumar

Hello everyone,

I hope you're all doing well. I recently launched an open-source project called the Ultimate JavaScript Project, and I'd love your support. Please check it out and give it a star on GitHub: Ultimate JavaScript Project. Your support would mean a lot to me and greatly help in the project's growth.

Thank you!