-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomplete-guide-to-python-1.py
144 lines (111 loc) · 3.35 KB
/
complete-guide-to-python-1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# Complete Introduction to Python
# 1. Strings & Printing them.
print("Hello World") # Print a simple string
print("Hey world") # Print another simple string
a = 5
print(a) # Print the integer value of variable 'a'
# 2. Basic Arithmetic.
num1 = 10
num2 = 5
print(num1 + num2) # Addition: 15
print(num1 - num2) # Subtraction: 5
print(num1 * num2) # Multiplication: 50
print(num1 / num2) # Division: 2.0
print(num1 ** num2) # Power: 100000
# You can also import the module 'math' for more advanced operations
import math
print(math.sqrt(num1)) # Square root: 3.1622776601683795
print(math.factorial(num2)) # Factorial: 120
print(math.gcd(num1, num2)) # Greatest common divisor: 5
# 3. Variables and Data Types
integer_var = 42 # Integer
float_var = 3.14 # Float
string_var = "Hello, Python!" # String
boolean_var = True # Boolean
print(type(integer_var)) # Output: <class 'int'>
print(type(float_var)) # Output: <class 'float'>
print(type(string_var)) # Output: <class 'str'>
print(type(boolean_var)) # Output: <class 'bool'>
# 4. Logical Operations
a = True
b = False
print(a and b) # Logical AND: False
print(a or b) # Logical OR: True
print(not a) # Logical NOT: False
# 5. Assignment Operations
x = 10
x += 5 # x = x + 5
print(x) # Output: 15
x -= 3 # x = x - 3
print(x) # Output: 12
x *= 2 # x = x * 2
print(x) # Output: 24
x /= 4 # x = x / 4
print(x) # Output: 6.0
# 6. Functions (Lambda, def, higher-order functions)
# Function using def
def greet(name):
return f"Hello, {name}!"
print(greet("Alice")) # Output: Hello, Alice!
# Lambda function
square = lambda x: x ** 2
print(square(4)) # Output: 16
# Higher-order function
def apply_function(f, x):
return f(x)
print(apply_function(square, 5)) # Output: 25
# 7. If Else
number = 7
if number > 0:
print("The number is positive.")
elif number < 0:
print("The number is negative.")
else:
print("The number is zero.")
# 8. Lists & Arrays
# List in Python
fruits = ["apple", "banana", "cherry"]
print(fruits) # Output: ['apple', 'banana', 'cherry']
# Array (using numpy)
import numpy as np
array = np.array([1, 2, 3, 4])
print(array) # Output: [1 2 3 4]
# 9. List Slicing
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print(numbers[2:5]) # Output: [3, 4, 5]
print(numbers[:3]) # Output: [1, 2, 3]
print(numbers[6:]) # Output: [7, 8, 9, 10]
# 10. File Handling
# Writing to a file
with open('example.txt', 'w') as file:
file.write("Hello, file handling!")
# Reading from a file
with open('example.txt', 'r') as file:
content = file.read()
print(content) # Output: Hello, file handling!
# 11. Importing Libraries
import math # Importing the math module
print(math.pi) # Output: 3.141592653589793
print(math.log(10)) # Output: 2.302585092994046
# 12. Exception Handling
try:
result = 10 / 0
except ZeroDivisionError:
print("Cannot divide by zero.")
else:
print("Division successful.")
finally:
print("Execution completed.")
# 13. Object-Oriented Programming
class Dog:
def __init__(self, name, age):
self.name = name
self.age = age
def bark(self):
return f"{self.name} says Woof!"
def get_age(self):
return self.age
# Creating an object of the Dog class
dog = Dog("Buddy", 5)
print(dog.bark()) # Output: Buddy says Woof!
print(dog.get_age()) # Output: 5