print("Hello, World!")
Data Type | Description | Example |
---|---|---|
int |
Integer | x = 5 |
float |
Floating-point number | y = 3.14 |
str |
String | name = "Alice" |
bool |
Boolean | is_valid = True |
list |
List | fruits = ["apple", "banana", "cherry"] |
tuple |
Tuple | point = (1, 2) |
dict |
Dictionary | person = {"name": "Alice", "age": 25} |
set |
Set | colors = {"red", "green", "blue"} |
Statement | Description | Example |
---|---|---|
if |
Conditional statement | if x > 0: |
elif |
Else if statement | elif x == 0: |
else |
Else statement | else: |
for |
For loop | for i in range(5): |
while |
While loop | while x > 0: |
break |
Break out of a loop | break |
continue |
Continue to the next iteration of a loop | continue |
Function | Description | Example |
---|---|---|
def |
Define a function | def greet(name): |
return |
Return a value from a function | return x + y |
Import | Description | Example |
---|---|---|
import |
Import a module | import math |
from ... import ... |
Import specific attributes from a module | from math import sqrt |
as |
Import a module with an alias | import numpy as np |
Operation | Description | Example |
---|---|---|
open |
Open a file | f = open("file.txt") |
read |
Read a file | content = f.read() |
write |
Write to a file | f.write("Hello, World!") |
close |
Close a file | f.close() |
with |
Context manager for file operations | with open("file.txt") as f: |
Statement | Description | Example |
---|---|---|
try |
Try to execute a block of code | try: |
except |
Handle exceptions | except Exception as e: |
finally |
Execute code regardless of exceptions | finally: |
Statement | Description | Example |
---|---|---|
class |
Define a class | class Dog: |
__init__ |
Initialize an instance of a class | def __init__(self, name): |
self |
Reference to the instance of the class | self.name = name |
method |
Define a method in a class | def bark(self): |
object |
Create an instance of a class | my_dog = Dog("Buddy") |
squares = [x**2 for x in range(10)]
Library | Description | Example |
---|---|---|
math |
Mathematical functions | import math |
random |
Generate random numbers | import random |
datetime |
Manipulate dates and times | import datetime |
os |
Interacting with the operating system | import os |
sys |
System-specific parameters and functions | import sys |
json |
JSON encoder and decoder | import json |