Python Basic Syntax

Print statement

The print() function is used to display text on the console.

Example:

print("Hello, World!")

Comments

Comments in Python start with the ‘#’ symbol and are used to add notes and explanations to your code. They are ignored by the Python interpreter and do not affect the execution of your program.

Example:

# This is a comment in Python
print("Hello World!") # This is another comment

Variables

In Python, you can create variables to store data. Variables are declared by assigning a value to a name, and their data type is inferred automatically based on the value assigned. Variable names can contain letters, numbers, and underscores, but cannot start with a number.

Example:

# Creating variables in Python
x = 5 # integer
y = "Hello" # string
z = True # boolean

Data Types

Python has several built-in data types, including integers, floats, booleans, strings, lists, tuples, and dictionaries. These data types can be used to store and manipulate different types of data.

Example:

# Data types in Python
x = 5 # integer
y = 3.14 # float
z = True # boolean
name = "John" # string
fruits = ["apple", "banana", "orange"] # list
t = (1, 2, 3) # tuple
person = {"name": "John", "age": 30} # dictionary

Conditional Statements

Conditional statements are used to execute different blocks of code based on a certain condition.

Example:

x = 10
if x > 5:
    print("x is greater than 5")
else:
    print("x is less than or equal to 5")

Loops

Loops are used to execute a block of code repeatedly.

Example:

for i in range(5):
    print(i)

Functions

Functions are reusable blocks of code that perform a specific task.

Example:

def greet(name):
    print("Hello, " + name + "!")

greet("John")

Python Docstrings

Python also has multi-line commenting capability. Docstrings can be one line, or multi-line.
Python uses Tripple quotes at the beginning and end of the docstrings.

"""This is a 
multiline docstring."""
print("Hello, World!") 

These are just a few examples of the basic syntax in Python. Python has a wide variety of built-in functions and modules, as well as an extensive library of third-party packages, making it a versatile and powerful language.

Wordpress Social Share Plugin powered by Ultimatelysocial
Wordpress Social Share Plugin powered by Ultimatelysocial