Basic Syntax Of Python by Copilot AI | Ballu Python 835430
Basic Syntax Of Python
Here are some basic syntax elements of Python:
### Comments
```python
# This is a single-line comment
"""
This is a multi-line comment
or docstring (used to describe functions, classes, and modules)
"""
```
### Variables and Data Types
```python
# Variables
x = 5
y = "Hello, World!"
# Data Types
integer = 10
floating_point = 10.5
string = "Python"
boolean = True
list_example = [1, 2, 3]
tuple_example = (1, 2, 3)
dictionary_example = {"key": "value"}
```
### Control Structures
#### If-Else Statements
```python
# If-Else
if x > 0:
print("x is positive")
else:
print("x is non-positive")
```
#### Loops
```python
# For Loop
for i in range(5):
print(i)
# While Loop
count = 0
while count < 5:
print(count)
count += 1
```
### Functions
```python
# Function Definition
def greet(name):
return f"Hello, {name}!"
# Function Call
print(greet("Alice"))
```
### Classes and Objects
```python
# Class Definition
class Dog:
def __init__(self, name, age):
self.name = name
self.age = age
def bark(self):
return "Woof!"
# Object Instantiation
my_dog = Dog("Buddy", 3)
print(my_dog.bark())
```
### Importing Modules
```python
# Importing a Module
import math
# Using a Module
print(math.sqrt(16))
```
These examples cover the basic syntax of Python.
Comments
Post a Comment