Skip to content

Commit 34196fd

Browse files
committed
Questions.py
1 parent dcdfd4b commit 34196fd

24 files changed

+520
-363
lines changed
File renamed without changes.
File renamed without changes.

Notes/Basic/3.keywords.py renamed to Notes/1.Basic/3.keywords.py

Lines changed: 177 additions & 172 deletions
Large diffs are not rendered by default.
Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# Data types
2-
# Data types are the classification or categorization of data items.
2+
# Data types are the classification or categorization of data items.
33
# It represents the kind of value that tells what operations can be performed on a particular data.
44
# In programming, data type is an important concept.
55

66
# Variables can store data of different types, and different types can do different things.
7-
# Since everything is an object in Python programming, data types are classes and variables
7+
# Since everything is an object in Python programming, data types are classes and variables
88
# are instances (objects) of these classes.
99

1010

@@ -23,45 +23,45 @@
2323

2424
# Getting the Data Type
2525
# What is Python type() Function?
26-
# To define the values ​​of various data types and check their data types we use the type() function.
26+
# To define the values ​​of various data types and check their data types we use the type() function.
2727
# Consider the following examples.
2828

29-
# This code assigns variable ‘x’ different values of various data types in Python.
30-
# It covers string, integer, float, complex, list, tuple, range, dictionary, set,
31-
# frozenset, boolean, bytes, bytearray, memoryview, and the special value ‘None’ successively.
32-
# Each assignment replaces the previous value, making ‘x’ take on the data type and value
29+
# This code assigns variable ‘x’ different values of various data types in Python.
30+
# It covers string, integer, float, complex, list, tuple, range, dictionary, set,
31+
# frozenset, boolean, bytes, bytearray, memoryview, and the special value ‘None’ successively.
32+
# Each assignment replaces the previous value, making ‘x’ take on the data type and value
3333
# of the most recent assignment.
3434

3535
x = "Hello World"
36-
print(x,"-",type(x))
36+
print(x, "-", type(x))
3737
x = 16
38-
print(x,"-",type(x))
38+
print(x, "-", type(x))
3939
x = 0.5
40-
print(x,"-",type(x))
41-
x = 3+5j
42-
print(x,"-",type(x))
40+
print(x, "-", type(x))
41+
x = 3 + 5j
42+
print(x, "-", type(x))
4343
x = ["Unknown", "Coder"]
44-
print(x,"-",type(x))
45-
x = ("Code","Hacker")
46-
print(x,"-",type(x))
47-
x = range(10)
48-
print(x,"-",type(x))
49-
x = {"name": "harrish", "age": 23,"Qualification":"MCA"}
50-
print(x,"-",type(x))
44+
print(x, "-", type(x))
45+
x = ("Code", "Hacker")
46+
print(x, "-", type(x))
47+
x = range(10)
48+
print(x, "-", type(x))
49+
x = {"name": "harrish", "age": 23, "Qualification": "MCA"}
50+
print(x, "-", type(x))
5151
x = {"harrish", "23", "MCA"}
52-
print(x,"-",type(x))
52+
print(x, "-", type(x))
5353
x = frozenset({"harrish", "23", "MCA"})
54-
print(x,"-",type(x))
54+
print(x, "-", type(x))
5555
x = True
56-
print(x,"-",type(x))
56+
print(x, "-", type(x))
5757
x = b"Harrish"
58-
print(x,"-",type(x))
58+
print(x, "-", type(x))
5959
x = bytearray(4)
60-
print(x,"-",type(x))
61-
x = memoryview(bytes(6))
62-
print(x,"-",type(x))
60+
print(x, "-", type(x))
61+
x = memoryview(bytes(6))
62+
print(x, "-", type(x))
6363
x = None
64-
print(x,"-",type(x))
64+
print(x, "-", type(x))
6565

6666
# Hello World - <class 'str'>
6767
# 16 - <class 'int'>
@@ -83,10 +83,9 @@
8383
# Python Numeric Data type:
8484
# In Python, numeric data type is used to hold numeric values.
8585

86-
# Integers, floating-point numbers and complex numbers fall under Python numbers category.
86+
# Integers, floating-point numbers and complex numbers fall under Python numbers category.
8787
# They are defined as int, float and complex classes in Python.
8888

8989
# int - holds signed integers of non-limited length.
9090
# float - holds floating decimal points and it's accurate up to 15 decimal places.
9191
# complex - holds complex numbers.
92-

Notes/1.Basic/Question.py

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
# What is Programming?
2+
# Programming is the process of writing instructions that a computer
3+
# can follow to perform tasks like calculations, data processing, automation, etc.
4+
5+
# Think of it as writing a recipe for a computer.
6+
7+
# Example:
8+
print("Hello, world!")
9+
# You’re telling the computer: “Display the text ‘Hello, world!’ on the screen.”
10+
11+
# What is Syntax?
12+
# Syntax is the set of rules that define how you write code correctly in a programming
13+
# language.
14+
15+
# Like grammar in English — you can’t write "are you how?" instead of "how are you?"
16+
17+
# Incorrect Syntax (will cause error):
18+
# print "Hello"
19+
20+
# Correct Syntax:
21+
print("Hello")
22+
23+
# What is Programming Code?
24+
# Programming Code (or source code) is the set of instructions 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+
# What is a Programming Language?
33+
# A programming language is a way to communicate with a computer using specific keywords, rules, and symbols.
34+
# Python is one such language—others include JavaScript, C++, Java, etc.
35+
# Python is known for being:
36+
# Easy to read
37+
# Powerful
38+
# Great for beginners and pros alike
39+
40+
# What is Semantics?
41+
# Semantics is the meaning behind your code.
42+
# Syntax is how you write it; semantics is what it means.
43+
44+
# Example:
45+
print("5" + "10") # Output: "510"
46+
47+
# Correct syntax, but the semantics might be wrong if you expected addition (you got string concatenation instead).
48+
49+
# What is a Script?
50+
# A script is a file with programming code (usually .py in Python) that runs line-by-line to perform tasks.
51+
52+
# You can run Python scripts to automate tasks like:
53+
# Sending emails
54+
# Renaming files
55+
# Scraping websites
56+
57+
# Example (saved as hello.py):
58+
# print("Running a script!")
59+
60+
# What is Automation?
61+
# Automation means writing code to let the computer do tasks for you without manual effort.
62+
63+
# Example: Rename 1000 files instead of doing it one by one.
64+
65+
# Example:
66+
67+
for i in range(1, 4):
68+
with open(f"file{i}.txt", "w") as f:
69+
f.write("Created by script")
70+
71+
# What is a Function?
72+
# A function is a reusable block of code that performs a task.
73+
74+
75+
# Example:
76+
def greet(name):
77+
print("Hello", name)
78+
79+
80+
greet("John") # Output: Hello John
81+
# You define it once and can reuse it anywhere.
82+
83+
# What is a Variable?
84+
# A variable is a container for storing data (like a label you stick on a value).
85+
86+
# Example:
87+
age = 25
88+
name = "Alice"
89+
# Here, age holds a number, and name holds text.
90+
91+
# What is a Generic Label (Variable Name)?
92+
# It’s just another way of referring to a variable name.
93+
# Use clear, meaningful names.
94+
95+
# Example:
96+
temperature = 98.6
97+
username = "bob123"
98+
# Avoid vague labels like x or data1 unless truly generic.
99+
100+
# What is print() in Python?
101+
# The print() function displays output to the screen (like debugging or showing results).
102+
103+
# Example:
104+
print("Hello!")
105+
106+
# What is an f-string?
107+
# f-strings (formatted strings) let you insert variable values directly inside strings using {}.
108+
109+
# Example:
110+
name = "Alice"
111+
age = 30
112+
print(f"My name is {name} and I am {age} years old.")
113+
# Much cleaner than:
114+
115+
print("My name is " + name + " and I am " + str(age) + " years old.")
116+
117+
# What is Implicit Conversion?
118+
# Python automatically converts data types when safe.
119+
120+
# Example:
121+
x = 5 # int
122+
y = 2.0 # float
123+
result = x + y
124+
print(result) # Output: 7.0 (converted to float automatically)
125+
126+
# What is Explicit Conversion?
127+
# You manually convert one data type to another using functions like int(), str(), float(), etc.
128+
129+
# Example:
130+
age = "25"
131+
age_num = int(age) # Convert string to integer
132+
print(age_num + 5) # Output: 30
133+
134+
# What is an Expression?
135+
# An expression is a piece of code that produces a value.
136+
137+
# Can be math, function calls, logic, etc.
138+
139+
# Example:
140+
x = 5 + 3 # Expression: 5 + 3
141+
print(len("hello")) # Expression: len("hello")
File renamed without changes.
File renamed without changes.

Notes/3.Advance/2.Higher_Order_Function.py

Whitespace-only changes.

Notes/3.Advance/3.Closure.py

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
# Closure :
2+
# Before seeing what a closure is,
3+
# we have to first understand what nested functions and non-local variables are.
4+
5+
# What are Python Closures
6+
7+
# A Closure in Python is a function object that remembers values in enclosing scopes even
8+
# if they are not present in memory.
9+
10+
# It is a record that stores a function together with an environment:
11+
# a mapping associating each free variable of the function
12+
# (variables that are used locally but defined in an enclosing scope) with the value
13+
# or reference to which the name was bound when the closure was created.
14+
# A closure—unlike a plain function—allows the function to access those captured variables
15+
# through the closure’s copies of their values or references,
16+
# even when the function is invoked outside their scope.
17+
18+
19+
# example 1: This is not closure
20+
def outerFunction():
21+
text = "hi"
22+
23+
def innerFunction():
24+
print("Inside Function")
25+
# text is free variable
26+
print(text)
27+
# text is free variable it can be access inside a innerfunction
28+
29+
# Note we are returning function
30+
# WITH parenthesis
31+
return innerFunction() # returning and executing innerfunction
32+
33+
34+
outerFunction()
35+
36+
37+
# Example 2:
38+
# Python program to illustrate
39+
# closures
40+
def outerFunction():
41+
text = "Hey"
42+
43+
def innerFunction():
44+
# text is free variable
45+
print(text)
46+
47+
# Note we are returning function
48+
# WITHOUT parenthesis it will not execute nothing
49+
return innerFunction
50+
51+
52+
myFunction = outerFunction()
53+
# It will execute everything inside a outerfunction and return innerFunction
54+
print(myFunction)
55+
# myFunction is a function now because we only returned not execute the innerfunction
56+
print(myFunction.__name__)
57+
# display inner function name on it
58+
myFunction() # Hey
59+
myFunction() # Hey
60+
myFunction() # Hey
61+
62+
# It print out message.
63+
# we are done with executing outer function.
64+
# but the inner function has still has access to that text variable that is it printing out.
65+
# Thats what closure is In simple terms a closure is a inner function that remembers and has
66+
# access to variable in the local scope in which it was created even after the outer function
67+
# has finished executing
68+
69+
70+
# Example 3: Add parameter to a function
71+
def outerFunction(msg):
72+
message = msg
73+
74+
def innerFunction():
75+
print(message)
76+
77+
return innerFunction
78+
79+
80+
hi_Function = outerFunction("Hi!")
81+
hello_Function = outerFunction("Hello!")
82+
hi_Function() # Hi!
83+
hello_Function() # Hello!
84+
85+
# One thing you must notice that we stil innerfunction still doesnot take any argument
86+
# to execute this function only need paranthesis.
87+
# when call this each function print is own values
88+
# that closure closes over the free variable from their environment
89+
# In this case message would be that free variable.
90+
91+
92+
# Example 3:
93+
94+
import logging
95+
96+
logging.basicConfig(filename="example.log", level=logging.INFO) # create logging file
97+
98+
99+
def logger(func):
100+
def logfun(*args):
101+
# *args= any no of arguments can be passed
102+
logging.info('running"{}"with arguments {}'.format(func.__name__, args))
103+
# print loggin info in example.log file on it
104+
print(func(*args))
105+
# call those func name passed in outer function and pass the inner function *args values
106+
107+
return logfun # it only return the inner function not executed
108+
109+
110+
def add(x, y):
111+
return x + y
112+
113+
114+
def sub(x, y):
115+
return x - y
116+
117+
118+
add_log = logger(add)
119+
# outer function executes and pass the argument
120+
sub_log = logger(sub)
121+
122+
add_log(10, 20)
123+
# inner function still have access to the outer function after execution it remember the inner function
124+
add_log(3, 3)
125+
# so it can easy to access the free variabe on it
126+
127+
sub_log(20, 10)
128+
sub_log(5, 2)
129+
sub_log(5, 2)
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)