|
| 1 | +# What is Programming? |
| 2 | +# Programming is the process of writing instructions that a computer |
| 3 | +# can follow to perform tasks like calculations, |
| 4 | +# data processing, automation, etc. |
| 5 | + |
| 6 | +# Think of it as writing a recipe for a computer. |
| 7 | + |
| 8 | +# Example: |
| 9 | +print("Hello, world!") |
| 10 | +# You’re telling the computer: |
| 11 | +# “Display the text ‘Hello, world!’ on the screen.” |
| 12 | + |
| 13 | +# What is a Programming Language? |
| 14 | +# A programming language is a way to communicate with a computer |
| 15 | +# using specific keywords, rules, and symbols. |
| 16 | +# Python is one such language—others include JavaScript, C++, Java, etc. |
| 17 | +# Python is known for being: |
| 18 | +# Easy to read |
| 19 | +# Powerful |
| 20 | +# Great for beginners and pros alike |
| 21 | + |
| 22 | +# What is Programming Code? |
| 23 | +# Programming Code (or source code) is the set of instructions |
| 24 | +# you write to build a program. |
| 25 | +# It's what the computer runs. |
| 26 | + |
| 27 | +# Example: |
| 28 | +name = "Alice" |
| 29 | +print("Hello, " + name) |
| 30 | +# This is a program written using Python code. |
| 31 | + |
| 32 | + |
| 33 | +# What is Syntax? |
| 34 | +# Syntax is the set of rules that define how you write code correctly |
| 35 | +# in a programming language. |
| 36 | + |
| 37 | +# Like grammar in English — you can’t write "are you how?" |
| 38 | +# instead of "how are you?" |
| 39 | + |
| 40 | +# Incorrect Syntax (will cause error): |
| 41 | +# print "Hello" |
| 42 | + |
| 43 | +# Correct Syntax: |
| 44 | +print("Hello") |
| 45 | + |
| 46 | +# What is Semantics? |
| 47 | +# Semantics is the meaning behind your code. |
| 48 | +# Syntax is how you write it; semantics is what it means. |
| 49 | + |
| 50 | +# Example: |
| 51 | +print("5" + "10") # Output: "510" |
| 52 | + |
| 53 | +# Correct syntax, but the semantics might be wrong if you expected addition |
| 54 | +# (you got string concatenation instead). |
| 55 | + |
| 56 | +# What is a Script? |
| 57 | +# A script is a file with programming code (usually .py in Python) |
| 58 | +# that runs line-by-line to perform tasks. |
| 59 | + |
| 60 | +# You can run Python scripts to automate tasks like: |
| 61 | +# Sending emails |
| 62 | +# Renaming files |
| 63 | +# Scraping websites |
| 64 | + |
| 65 | +# Example (saved as hello.py): |
| 66 | +# print("Running a script!") |
| 67 | + |
| 68 | +# What is Automation? |
| 69 | +# Automation means writing code to let the computer |
| 70 | +# do tasks for you without manual effort. |
| 71 | + |
| 72 | +# Example: Rename 1000 files instead of doing it one by one. |
| 73 | + |
| 74 | +# Example: |
| 75 | + |
| 76 | +for i in range(1, 4): |
| 77 | + with open(f"file{i}.txt", "w") as f: |
| 78 | + f.write("Created by script") |
| 79 | + |
| 80 | + |
| 81 | +# What is a Function? |
| 82 | +# A function is a reusable block of code that performs a task. |
| 83 | + |
| 84 | + |
| 85 | +# Example: |
| 86 | +def greet(name): |
| 87 | + print("Hello", name) |
| 88 | + |
| 89 | + |
| 90 | +greet("John") # Output: Hello John |
| 91 | +# You define it once and can reuse it anywhere. |
| 92 | + |
| 93 | +# What is a Variable? |
| 94 | +# A variable is a container for storing data |
| 95 | +# (like a label you stick on a value). |
| 96 | + |
| 97 | +# Example: |
| 98 | +age = 25 |
| 99 | +name = "Alice" |
| 100 | +# Here, age holds a number, and name holds text. |
| 101 | + |
| 102 | +# What is a Generic Label (Variable Name)? |
| 103 | +# It’s just another way of referring to a variable name. |
| 104 | +# Use clear, meaningful names. |
| 105 | + |
| 106 | +# Example: |
| 107 | +temperature = 98.6 |
| 108 | +username = "bob123" |
| 109 | +# Avoid vague labels like x or data1 unless truly generic. |
| 110 | + |
| 111 | +# What is print() in Python? |
| 112 | +# The print() function displays output to the screen |
| 113 | +# (like debugging or showing results). |
| 114 | + |
| 115 | +# Example: |
| 116 | +print("Hello!") |
| 117 | + |
| 118 | +# What is an f-string? |
| 119 | +# f-strings (formatted strings) |
| 120 | +# let you insert variable values directly inside strings using {}. |
| 121 | + |
| 122 | +# Example: |
| 123 | +name = "Alice" |
| 124 | +age = 30 |
| 125 | +print(f"My name is {name} and I am {age} years old.") |
| 126 | +# Much cleaner than: |
| 127 | + |
| 128 | +print("My name is " + name + " and I am " + str(age) + " years old.") |
| 129 | + |
| 130 | +# What is Implicit Conversion? |
| 131 | +# Python automatically converts data types when safe. |
| 132 | + |
| 133 | +# Example: |
| 134 | +x = 5 # int |
| 135 | +y = 2.0 # float |
| 136 | +result = x + y |
| 137 | +print(result) # Output: 7.0 (converted to float automatically) |
| 138 | + |
| 139 | +# What is Explicit Conversion? |
| 140 | +# You manually convert one data type to another using functions |
| 141 | +# like int(), str(), float(), etc. |
| 142 | + |
| 143 | +# Example: |
| 144 | +age = "25" |
| 145 | +age_num = int(age) # Convert string to integer |
| 146 | +print(age_num + 5) # Output: 30 |
| 147 | + |
| 148 | +# What is an Expression? |
| 149 | +# An expression is a piece of code that produces a value. |
| 150 | + |
| 151 | +# Can be math, function calls, logic, etc. |
| 152 | + |
| 153 | +# Example: |
| 154 | +x = 5 + 3 # Expression: 5 + 3 |
| 155 | +print(len("hello")) # Expression: len("hello") |
| 156 | + |
| 157 | + |
| 158 | +# What is a Data Type? |
| 159 | +# In Python (and all programming languages), |
| 160 | +# a data type defines the kind of value a variable holds. |
| 161 | +# Think of it like different types of containers: |
| 162 | +# one for water (liquid), one for rice (solid), one for air (gas). |
| 163 | +# You need the right container (or type) for the right content. |
| 164 | + |
| 165 | +# In the same way, Python needs to know the type of data you're working with so |
| 166 | +# it can handle it correctly — like doing math, storing text, |
| 167 | +# looping through items, etc. |
| 168 | + |
| 169 | +# Python Data Types (Main Categories) |
| 170 | +# Python has several built-in data types. Here's a breakdown: |
| 171 | + |
| 172 | +# 1. Numeric Types: For numbers |
| 173 | +# a. int – Integer |
| 174 | +# Whole numbers (positive, negative, or zero) |
| 175 | + |
| 176 | +# Example: |
| 177 | +age = 25 |
| 178 | + |
| 179 | +# b. float – Floating-point (decimal) number |
| 180 | +# Numbers with decimal points |
| 181 | + |
| 182 | +# Example: |
| 183 | +# price = 99.99 |
| 184 | + |
| 185 | +# c. complex – Complex number (used in math) |
| 186 | +# Numbers with real and imaginary parts |
| 187 | + |
| 188 | +# Example: |
| 189 | +# z = 3 + 5j |
| 190 | + |
| 191 | +# 2. Text Type |
| 192 | +# str – String |
| 193 | +# A sequence of characters (letters, words, sentences) |
| 194 | + |
| 195 | +# Written in quotes ' ' or " " |
| 196 | + |
| 197 | +# Example: |
| 198 | +name = "Alice" |
| 199 | +greeting = "Hello, world!" |
| 200 | + |
| 201 | +# 3. Boolean Type |
| 202 | +# bool – Boolean |
| 203 | +# Only two values: True or False |
| 204 | +# Used in decision making (like if-statements) |
| 205 | + |
| 206 | +# Example: |
| 207 | +is_sunny = True |
| 208 | +has_permission = False |
| 209 | + |
| 210 | +# 4. Sequence Types |
| 211 | +# These store collections of items. |
| 212 | + |
| 213 | +# a. list – Ordered, changeable (mutable), allows duplicates |
| 214 | + |
| 215 | +# Example: |
| 216 | +fruits = ["apple", "banana", "cherry"] |
| 217 | + |
| 218 | +# b. tuple – Ordered, unchangeable (immutable), allows duplicates |
| 219 | + |
| 220 | +# Example: |
| 221 | +dimensions = (1920, 1080) |
| 222 | + |
| 223 | +# c. range – Sequence of numbers, usually used in loops |
| 224 | + |
| 225 | +# Example: |
| 226 | +numbers = range(5) # same as [0, 1, 2, 3, 4] |
| 227 | + |
| 228 | +# 5. Mapping Type |
| 229 | +# dict – Dictionary |
| 230 | +# Stores key-value pairs |
| 231 | +# Unordered (as of Python 3.6+, it maintains insertion order) |
| 232 | + |
| 233 | +# Example: |
| 234 | +person = {"name": "John", "age": 30, "city": "New York"} |
| 235 | + |
| 236 | +# 6. Set Types |
| 237 | +# Used for storing unique items. |
| 238 | +# a. set – Unordered, no duplicates |
| 239 | + |
| 240 | +# Example: |
| 241 | +colors = {"red", "green", "blue"} |
| 242 | + |
| 243 | +# b. frozenset – Like a set, but unchangeable |
| 244 | + |
| 245 | +# 7. Binary Types |
| 246 | +# Used when working with binary data |
| 247 | +# (e.g., files, images, or network communication). |
| 248 | + |
| 249 | +# bytes |
| 250 | + |
| 251 | +# bytearray |
| 252 | + |
| 253 | +# memoryview |
| 254 | + |
| 255 | +# Example: |
| 256 | +data = b"Hello" # bytes |
| 257 | + |
| 258 | +# NoneType |
| 259 | +# None – Special type that means “no value” |
| 260 | +# Example: |
| 261 | +x = None |
| 262 | + |
| 263 | + |
| 264 | +# Table |
| 265 | +# Data Type Example Description |
| 266 | +# int 42 Whole numbers |
| 267 | +# float 3.14 Decimal numbers |
| 268 | +# complex 2 + 3j Complex numbers |
| 269 | +# str "hello" Text/characters |
| 270 | +# bool True, False True or False values |
| 271 | +# list [1, 2, 3] Ordered, changeable sequence |
| 272 | +# tuple (1, 2, 3) Ordered, unchangeable sequence |
| 273 | +# dict {"key": "value"} Key-value pairs |
| 274 | +# set {1, 2, 3} Unordered, unique elements |
| 275 | +# frozenset frozenset([1, 2, 3]) Immutable set |
| 276 | +# bytes b"abc" Binary data |
| 277 | +# NoneType None No value |
| 278 | + |
| 279 | +# How to Check a Variable’s Data Type |
| 280 | +# You can use the type() function: |
| 281 | + |
| 282 | +x = 10 |
| 283 | +print(type(x)) # Output: <class 'int'> |
0 commit comments