-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPython_Basics.py
More file actions
208 lines (134 loc) · 5.63 KB
/
Copy pathPython_Basics.py
File metadata and controls
208 lines (134 loc) · 5.63 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# # 1. Introduction to Python 🐍
# Python is a high-level, interpreted, general-purpose programming language. Its design philosophy emphasizes code readability with its notable use of significant indentation. It's dynamically-typed and garbage-collected.
# Interpreted: Code is executed line by line, which makes debugging easier.
# High-Level: Abstracts away complex details of the computer's hardware.
# Dynamically-Typed: You don't need to declare the type of a variable when you create it.
# General-Purpose: Can be used for web development, data science, automation, and more.
# ## 2. Basic Syntax and Variables
# Python syntax is clean and readable.
# Indentation: Python uses indentation (usually 4 spaces) to define blocks of code, instead of curly braces {}.
# Comments: Use the hash symbol # for single-line comments. For multi-line comments, you can use triple quotes ''' or """.
# Variables: Variables are containers for storing data values. You create a variable by assigning a value to it using the equals sign =.
# Python
# This is a comment
x = 10 # x is an integer
name = "Alice" # name is a string
pi = 3.14 # pi is a float
# Python is case-sensitive
age = 25
Age = 30 # 'age' and 'Age' are different variables
## 3. Data Types
# Python has several built-in data types.
# Primitive Types
# Integers (int): Whole numbers, like 10, -5, 0.
# Floats (float): Numbers with a decimal point, like 3.14, -0.001.
# Strings (str): Sequences of characters, enclosed in single ' or double " quotes.
# Booleans (bool): Represent truth values, either True or False.
# Data Structures (Collections)
# Lists (list): Ordered, mutable (changeable) collections of items. Enclosed in square brackets [].
# Python
my_list = [1, "hello", 3.14, True]
my_list[1] = "world" # Lists are mutable
print(my_list) # Output: [1, 'world', 3.14, True]
# Tuples (tuple): Ordered, immutable (unchangeable) collections. Enclosed in parentheses ().
# Python
my_tuple = (1, "hello", 3.14)
# my_tuple[1] = "world" # This would cause an error
# Dictionaries (dict): Unordered collections of key-value pairs. Enclosed in curly braces {}. Keys must be unique and immutable.
# Python
my_dict = {"name": "Bob", "age": 30}
print(my_dict["name"]) # Output: Bob
# Sets (set): Unordered, unindexed collections of unique items. Enclosed in curly braces {}.
# Python
my_set = {1, 2, 3, 3, 4}
print(my_set) # Output: {1, 2, 3, 4}
# 4. Control Flow
# Control flow statements direct the order in which code is executed.
# Conditional Statements
# Used to perform different actions based on different conditions.
# Python
x = 10
if x > 5:
print("x is greater than 5")
elif x == 5:
print("x is 5")
else:
print("x is less than 5")
# Loops
# Used to execute a block of code repeatedly.
# for loop: Iterates over a sequence (like a list, tuple, or string).
# Python
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)
# while loop: Executes a block of code as long as a condition is true.
# Python
count = 0
while count < 5:
print(count)
count += 1 # Increment count
# 5. Functions
# A function is a block of code that only runs when it is called. You can pass data, known as parameters, into a function.
# Defining a function: Use the def keyword.
# Returning a value: Use the return keyword.
# Python
# A simple function
def greet(name):
"""This function greets the person passed in as a parameter."""
print(f"Hello, {name}!")
# Function with a return value
def add_numbers(x, y):
return x + y
# Calling functions
greet("Charlie") # Output: Hello, Charlie!
result = add_numbers(5, 3)
print(result) # Output: 8
## 6. Object-Oriented Programming (OOP)
# Python is an object-oriented programming language. Almost everything in Python is an object, with its properties and methods.
# Class: A blueprint for creating objects.
# Object: An instance of a class.
# Python
class Dog:
# The __init__() function is a constructor, called when an object is created
def __init__(self, name, age):
self.name = name
self.age = age
def bark(self):
return "Woof!"
# Creating an object (instance) of the Dog class
my_dog = Dog("Buddy", 4)
print(my_dog.name) # Accessing object properties
print(my_dog.bark()) # Calling object methods
## 7. Modules and Packages
# Module: A file containing Python code (functions, classes, etc.). You can import it into other files to reuse its code.
# Package: A collection of modules.
# You use the import statement to use code from a module.
# Python
# import the 'math' module
import math
print(math.sqrt(16)) # Output: 4.0
# You can also import specific functions
from datetime import datetime
print(datetime.now())
## 8. File Handling 📂
# Python has functions for creating, reading, updating, and deleting files.
# Use the open() function to open a file. It takes two parameters: the filename and the mode ('r' for read, 'w' for write, 'a' for append).
# It's good practice to use the with statement because it automatically closes the file for you.
# Python
# Writing to a file (will create the file if it doesn't exist)
with open("example.txt", "w") as file:
file.write("Hello, file!")
# Reading from a file
with open("example.txt", "r") as file:
content = file.read()
print(content)
## 9. Exception Handling
# When an error occurs during execution, it's called an exception. You can use try...except blocks to handle these errors gracefully instead of letting your program crash.
# Python
try:
result = 10 / 0
except ZeroDivisionError:
print("Error: You cannot divide by zero!")
finally:
# This block executes regardless of whether an error occurred or not
print("Execution finished.")