Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
6 views

Comprehensive Guide To Python Commands

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Comprehensive Guide To Python Commands

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Comprehensive Guide to Python Commands

Comprehensive Guide to Python Commands

1. Introduction to Python

Python is a high-level, interpreted programming language known for its readability and ease of use.

2. Basic Syntax

print("Hello, World!") # Outputs: Hello, World!

3. Variables and Data Types

# Variable assignment

x=5

y = "Hello"

z = 3.14

# Data types

a = 10 # int

b = 10.5 # float

c = "Python" # str

d = [1, 2, 3] # list

e = (1, 2, 3) # tuple

f = {1, 2, 3} # set

g = {"a": 1, "b": 2} # dict

4. Operators
Comprehensive Guide to Python Commands

# Arithmetic operators

x=5

y=2

print(x + y) # 7

print(x - y) # 3

print(x * y) # 10

print(x / y) # 2.5

print(x % y) # 1

# Comparison operators

print(x == y) # False

print(x != y) # True

print(x > y) # True

print(x < y) # False

# Logical operators

print(x > 1 and y < 5) # True

print(x > 1 or y > 5) # True

print(not (x > 1)) # False

5. Control Flow

# if statement

x = 10

if x > 5:

print("x is greater than 5")


Comprehensive Guide to Python Commands

# for loop

for i in range(5):

print(i)

# while loop

i=0

while i < 5:

print(i)

i += 1

6. Functions

def greet(name):

return f"Hello, {name}"

print(greet("Alice")) # Outputs: Hello, Alice

7. Modules and Packages

# Importing a module

import math

print(math.sqrt(16)) # 4.0

# Importing specific functions

from math import sqrt

print(sqrt(25)) # 5.0
Comprehensive Guide to Python Commands

8. File Handling

# Writing to a file

with open("example.txt", "w") as file:

file.write("Hello, World!")

# Reading from a file

with open("example.txt", "r") as file:

content = file.read()

print(content) # Outputs: Hello, World!

9. Exception Handling

try:

result = 10 / 0

except ZeroDivisionError:

print("You cannot divide by zero!")

10. Object-Oriented Programming

class Dog:

def __init__(self, name):

self.name = name

def bark(self):

return f"{self.name} is barking"


Comprehensive Guide to Python Commands

dog = Dog("Rex")

print(dog.bark()) # Outputs: Rex is barking

11. Standard Library Modules

# datetime module

import datetime

now = datetime.datetime.now()

print(now)

# os module

import os

print(os.getcwd()) # Outputs the current working directory

12. Commonly Used Libraries

# numpy for numerical operations

import numpy as np

arr = np.array([1, 2, 3])

print(arr * 2) # Outputs: [2 4 6]

# pandas for data manipulation

import pandas as pd

df = pd.DataFrame({"A": [1, 2], "B": [3, 4]})

print(df)

13. Advanced Topics


Comprehensive Guide to Python Commands

# List comprehensions

squares = [x**2 for x in range(10)]

print(squares) # Outputs: [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

# Lambda functions

add = lambda x, y: x + y

print(add(5, 3)) # Outputs: 8

# Decorators

def decorator_func(func):

def wrapper():

print("Function is being called")

func()

print("Function has been called")

return wrapper

@decorator_func

def hello():

print("Hello, World!")

hello()

# Generators

def my_generator():

yield 1
Comprehensive Guide to Python Commands

yield 2

yield 3

gen = my_generator()

for value in gen:

print(value) # Outputs: 1 2 3

# Context managers

with open("example.txt", "w") as file:

file.write("Hello, World!")

You might also like