상세 컨텐츠

본문 제목

Python Coding Beginner's Guide

About IT

by thriveandshine 2023. 2. 13. 23:18

본문

반응형

computer

Python Coding

Python is a versatile language that you can use on the backend, frontend, or full stack of a web application. In this blog, we will explore the basics of Python coding.

Python Syntax

Python is a whitespace sensitive language. That means that the indentation of your code is important. Python uses four spaces for indentation.

Python also uses colons (:) and semicolons (;) to denote code blocks.

Here is a basic example of Python syntax:

def main():
    print("Hello, world!")

if __name__ == "__main__":
    main()

In the example above, we have a function called main. This function prints "Hello, world!".

We also have an if statement. The if statement checks if the name variable is equal to "main". If it is, then the code in the if block is executed.

Variables

In Python, you can create variables by simply assigning a value to a name.

my_variable = "Hello, world!"

You can also create variables without assigning a value. These variables will have the value None.

my_other_variable

You can also delete variables using the del keyword.

del my_variable

Data Types

Python has many built-in data types. These data types can be divided into two categories: mutable and immutable.

Mutable data types can be changed after they are created. Immutable data types cannot be changed after they are created.

Here are some of the most common data types in Python:

Numbers

Integers and floats are two of the most common numeric data types.

An integer is a whole number. A float is a number with a decimal point.

my_integer = 42
my_float = 3.14

You can also create complex numbers. A complex number is a number with a real and imaginary part.

my_complex = 3 + 4j

Booleans

A boolean is a data type that can have one of two values: True or False.

my_boolean = True

Strings

A string is a sequence of characters.

my_string = "Hello, world!"

You can also create strings using triple quotes. Triple quotes allow you to create strings that span multiple lines.

my_other_string = """This is a
multi-line string."""

Lists

A list is a mutable data type that is used to store a collection of items.

my_list = [1, 2, 3, 4, 5]

You can access items in a list using their index.

my_list[0] # This will return the first item in the list
my_list[1] # This will return the second item in the list
my_list[-1] # This will return the last item in the list

You can also add items to a list using the append() method.

my_list.append(6) # This will add the item 6 to the end of the list

Tuples

A tuple is an immutable data type that is used to store a collection of items.

my_tuple = (1, 2, 3, 4, 5)

You can access items in a tuple using their index.

my_tuple[0] # This will return the first item in the tuple
my_tuple[1] # This will return the second item in the tuple
my_tuple[-1] # This will return the last item in the tuple

Sets

A set is a data type that is used to store a collection of unique items.

my_set = {1, 2, 3, 4, 5}

You can add items to a set using the add() method.

my_set.add(6) # This will add the item 6 to the set

You can remove items from a set using the remove() method.

my_set.remove(6) # This will remove the item 6 from the set

Dictionaries

A dictionary is a data type that is used to store a collection of key-value pairs.

my_dictionary = {"key1": "value1", "key2": "value2"}

You can access values in a dictionary using their key.

my_dictionary["key1"] # This will return the value "value1"

You can also add items to a dictionary.

my_dictionary["key3"] = "value3" # This will add the key "key3" with the value "value3" to the dictionary
반응형

관련글 더보기