
Google Colab (or Google Colaboratory) is a powerful cloud-based platform provided by Google that enables users to write, execute, and share Python code through their web browser. Here are some of its key features and functions:
Key Features:
Jupyter Notebook Support:
Google Colab supports Jupyter notebooks, allowing users to create and share documents that contain live code, equations, visualizations, and narrative text.
Cloud-Based Environment:
Being cloud-based, Colab does not require any installation or setup. Users can start coding immediately without worrying about software compatibility or resource limitations.
Free Access to GPUs and TPUs:
Colab provides free access to powerful computing resources, including GPUs (Graphics Processing Units) and TPUs (Tensor Processing Units), which are essential for training machine learning models and performing computationally intensive tasks.
Collaborative Workspace:
Multiple users can collaborate on the same notebook in real-time, making it an excellent tool for team projects and learning environments.
Integration with Google Drive:
Notebooks can be saved directly to Google Drive, providing seamless integration with Google’s cloud storage services for easy access and sharing.
Pre-Installed Libraries:
Colab comes with many popular Python libraries pre-installed, such as TensorFlow, Keras, PyTorch, NumPy, and Pandas, saving users the time and effort needed to set up their environment.
Interactive Visualizations:
Users can create interactive visualizations using libraries like Matplotlib, Plotly, and Altair directly within the notebook, enhancing data exploration and presentation.
Key Functions:
Code Execution:
Run Python code cells in an interactive environment, with the ability to stop, restart, and reset the runtime as needed.
Markdown Support:
Combine code with Markdown to create rich text narratives, including formatted text, links, images, and LaTeX equations.
Data Import and Export:
Easily import data from various sources like CSV files, Google Sheets, and databases. Export results and data directly to Google Drive or local storage.
Customizable Environments:
Install additional libraries and packages using pip or apt-get commands to tailor the environment to specific project needs.
Version Control:
Integrate with GitHub to clone repositories, commit changes, and manage version control within the notebook interface.
Magic Commands:
Use IPython magic commands for advanced functionality, such as timing code execution (%timeit), running shell commands (!command), and accessing environment variables (%env).
Resource Monitoring:
Monitor CPU, RAM, and disk usage to optimize performance and manage resources effectively during computations.
Conclusion
Google Colab is a versatile and user-friendly platform that provides a rich set of features and functions tailored for data science, machine learning, and general Python programming. Its cloud-based nature, coupled with powerful computational resources and collaborative capabilities, makes it an invaluable tool for both beginners and experienced professionals.
Google Colab: विशेषताएँ और कार्य
Google Colab (या Google Colaboratory) गूगल द्वारा प्रदान किया गया एक शक्तिशाली क्लाउड-आधारित प्लेटफ़ॉर्म है, जो उपयोगकर्ताओं को अपने वेब ब्राउज़र के माध्यम से Python कोड लिखने, निष्पादित करने और साझा करने की अनुमति देता है। यहाँ इसकी कुछ प्रमुख विशेषताएँ और कार्य दिए गए हैं:
प्रमुख विशेषताएँ:
Jupyter Notebook सपोर्ट:
Google Colab Jupyter नोटबुक का समर्थन करता है, जिससे उपयोगकर्ता लाइव कोड, समीकरण, दृश्य और वर्णनात्मक पाठ वाले दस्तावेज़ बना और साझा कर सकते हैं।
क्लाउड-आधारित वातावरण:
क्लाउड-आधारित होने के कारण, Colab को किसी स्थापना या सेटअप की आवश्यकता नहीं होती है। उपयोगकर्ता तुरंत कोडिंग शुरू कर सकते हैं, बिना सॉफ़्टवेयर संगतता या संसाधन सीमाओं की चिंता किए।
मुफ्त GPU और TPU एक्सेस:
Colab शक्तिशाली कंप्यूटिंग संसाधनों, जैसे कि GPUs (Graphics Processing Units) और TPUs (Tensor Processing Units) तक मुफ्त पहुंच प्रदान करता है, जो मशीन लर्निंग मॉडल को प्रशिक्षण देने और गणनात्मक रूप से गहन कार्यों को निष्पादित करने के लिए आवश्यक हैं।
सहयोगी कार्यक्षेत्र:
कई उपयोगकर्ता एक ही नोटबुक पर वास्तविक समय में सहयोग कर सकते हैं, जिससे यह टीम परियोजनाओं और सीखने के वातावरण के लिए एक उत्कृष्ट उपकरण बन जाता है।
Google Drive के साथ एकीकरण:
नोटबुक को सीधे Google Drive पर सहेजा जा सकता है, जो आसान पहुंच और साझा करने के लिए गूगल के क्लाउड स्टोरेज सेवाओं के साथ निर्बाध एकीकरण प्रदान करता है।
पूर्व-स्थापित लाइब्रेरी:
Colab में कई लोकप्रिय Python लाइब्रेरी पहले से ही स्थापित होती हैं, जैसे TensorFlow, Keras, PyTorch, NumPy, और Pandas, जिससे उपयोगकर्ताओं को अपने वातावरण को सेटअप करने में समय और प्रयास की बचत होती है।
इंटरैक्टिव विज़ुअलाइज़ेशन:
उपयोगकर्ता नोटबुक के भीतर Matplotlib, Plotly, और Altair जैसी लाइब्रेरी का उपयोग करके इंटरैक्टिव विज़ुअलाइज़ेशन बना सकते हैं, जो डेटा अन्वेषण और प्रस्तुति को बढ़ाता है।
प्रमुख कार्य:
कोड निष्पादन:
एक इंटरैक्टिव वातावरण में Python कोड कोशिकाओं को चलाएं, जिसमें रनटाइम को रोकने, पुनरारंभ करने और रीसेट करने की क्षमता होती है।
Markdown सपोर्ट:
कोड को Markdown के साथ मिलाकर समृद्ध पाठ वर्णनाएं बनाएं, जिसमें स्वरूपित पाठ, लिंक, चित्र और LaTeX समीकरण शामिल हैं।
डेटा आयात और निर्यात:
CSV फ़ाइलों, Google Sheets और डेटाबेस जैसी विभिन्न स्रोतों से आसानी से डेटा आयात करें। परिणामों और डेटा को सीधे Google Drive या स्थानीय स्टोरेज में निर्यात करें।
अनुकूलन योग्य वातावरण:
pip या apt-get कमांड का उपयोग करके अतिरिक्त लाइब्रेरी और पैकेज इंस्टॉल करें ताकि वातावरण को विशिष्ट परियोजना आवश्यकताओं के अनुसार अनुकूलित किया जा सके।
वर्शन नियंत्रण:
GitHub के साथ एकीकृत करें ताकि रिपॉजिटरी को क्लोन किया जा सके, परिवर्तन कमिट किए जा सकें, और नोटबुक इंटरफ़ेस के भीतर वर्शन नियंत्रण का प्रबंधन किया जा सके।
मैजिक कमांड:
उन्नत कार्यक्षमता के लिए IPython मैजिक कमांड का उपयोग करें, जैसे कि कोड निष्पादन का समय (%timeit), शेल कमांड चलाना (!command), और वातावरण वेरिएबल तक पहुंचना (%env)।
संसाधन निगरानी:
गणनाओं के दौरान प्रदर्शन को अनुकूलित करने और संसाधनों का प्रभावी ढंग से प्रबंधन करने के लिए CPU, RAM, और डिस्क उपयोग की निगरानी करें।
निष्कर्ष
Google Colab एक बहुमुखी और उपयोगकर्ता-मित्रवत प्लेटफ़ॉर्म है जो डेटा विज्ञान, मशीन लर्निंग, और सामान्य Python प्रोग्रामिंग के लिए विशेष रूप से अनुकूलित सुविधाओं और कार्यों का एक समृद्ध सेट प्रदान करता है। इसकी क्लाउड-आधारित प्रकृति, शक्तिशाली कंप्यूटिंग संसाधनों और सहयोगात्मक क्षमताओं के साथ मिलकर, इसे शुरुआती और अनुभवी पेशेवरों दोनों के लिए एक अमूल्य उपकरण बनाती है।
Python Variables: A Comprehensive Overview
Variables are fundamental to programming, serving as containers for storing data values. In Python, variables are created the moment you assign a value to them. Python variables do not require explicit declaration to reserve memory space; the declaration happens automatically when a value is assigned.
Key Characteristics of Python Variables:
Dynamic Typing:
Python is a dynamically-typed language, which means you don't need to declare the type of a variable when you create one. The type is determined at runtime based on the value assigned.
Variable Naming Rules:
Variable names must start with a letter (a-z, A-Z) or an underscore (_).
The rest of the name can contain letters, digits (0-9), and underscores.
Variable names are case-sensitive (e.g., myVar, Myvar, and MYVAR are three different variables).
Certain keywords cannot be used as variable names (e.g., if, for, while, class).
Assigning Values:
You can assign values to variables using the assignment operator (=). For example
x = 5
name = "Alice"
is_active = True
Multiple variables can be assigned in a single line
a, b, c = 1, 2, 3
Variables in Python can be reassigned to different values and even different data types
x = 10
x = "Hello"
x = 3.14
पायथन वेरिएबल्स: एक व्यापक अवलोकन
प्रोग्रामिंग में वेरिएबल्स मौलिक होते हैं, जो डेटा मानों को संग्रहीत करने के लिए कंटेनर के रूप में कार्य करते हैं। पायथन में, वेरिएबल्स उस समय बनते हैं जब आप उन्हें एक मान सौंपते हैं। पायथन वेरिएबल्स को मेमोरी स्थान आरक्षित करने के लिए स्पष्ट घोषणा की आवश्यकता नहीं होती है; घोषणा स्वचालित रूप से तब होती है जब एक मान सौंपा जाता है।
पायथन वेरिएबल्स की मुख्य विशेषताएँ:
डायनामिक टाइपिंग:
पायथन एक डायनामिकली-टाइप्ड भाषा है, जिसका अर्थ है कि आपको एक वेरिएबल बनाने के समय उसके प्रकार की घोषणा करने की आवश्यकता नहीं होती। प्रकार रनटाइम पर सौंपे गए मान के आधार पर निर्धारित होता है।
वेरिएबल नामकरण नियम:
वेरिएबल के नाम एक अक्षर (a-z, A-Z) या अंडरस्कोर (_) से शुरू होने चाहिए।
नाम का बाकी हिस्सा अक्षरों, अंकों (0-9), और अंडरस्कोर को शामिल कर सकता है।
वेरिएबल नाम केस-संवेदनशील होते हैं (जैसे, myVar, Myvar, और MYVAR तीन अलग-अलग वेरिएबल्स हैं)।
कुछ कीवर्ड्स वेरिएबल नामों के रूप में उपयोग नहीं किए जा सकते (जैसे, if, for, while, class)।
मूल्यों को सौंपना:
आप असाइनमेंट ऑपरेटर (=) का उपयोग करके वेरिएबल्स को मान सौंप सकते हैं। उदाहरण के लिए:
x = 5
name = "Alice"
is_active = True
एक ही पंक्ति में कई वेरिएबल्स को सौंपा जा सकता है:
a, b, c = 1, 2, 3
वेरिएबल्स को पुनः सौंपना:
पायथन में वेरिएबल्स को विभिन्न मानों और यहां तक कि विभिन्न डेटा प्रकारों को पुनः सौंपा जा सकता है:
x = 10
x = "Hello"
x = 3.14
उदाहरण उपयोग:
# वेरिएबल्स को मान सौंपना
age = 25
name = "Alice"
price = 19.99
is_member = True
# वेरिएबल्स के मान प्रिंट करना
print(age) # Output: 25
print(name) # Output: Alice
print(price) # Output: 19.99
print(is_member) # Output: True
# वेरिएबल्स को पुनः सौंपना
age = 30
name = "Bob"
price = 29.99
is_member = False
print(age) # Output: 30
print(name) # Output: Bob
print(price) # Output: 29.99
print(is_member) # Output: False
Description of Python Comments and Data Types
Python Comments:
Comments are used in programming to include text within the code that explains parts of the code or adds notes. In Python, comments do not affect the execution of the code and are useful for anyone reading the code.
Single-line Comments:
A single-line comment is written using the # symbol:
# This is a single-line comment
x = 5 # This is an inline comment
Multi-line Comments:
In Python, a multi-line comment can be written using triple quotes ''' or """:
'''
This is a
multi-line
comment
'''
"""
This is also a
multi-line
comment
"""
Python Data Types:
Data types in Python are very important as they specify the type of data stored in variables. Various types of data types are available in Python:
Primitive Data Types:
Integer: Whole numbers (e.g., x = 10)
Float: Decimal numbers (e.g., y = 3.14)
String: Sequence of characters (e.g., name = "Alice")
Boolean: True or False values (e.g., is_active = True)
Collection Data Types:
List: An ordered collection of items (e.g., fruits = ["apple", "banana", "cherry"])
Tuple: An ordered, immutable collection of items (e.g., coordinates = (10, 20))
Dictionary: A collection of key-value pairs (e.g., person = {"name": "Alice", "age": 25})
Set: An unordered collection of unique items (e.g., unique_numbers = {1, 2, 3})
Example Usage:
Data Types Example:
# Integer
age = 25
# Float
price = 19.99
# String
name = "Alice"
# Boolean
is_member = True
# List
fruits = ["apple", "banana", "cherry"]
# Tuple
coordinates = (10, 20)
# Dictionary
person = {"name": "Alice", "age": 25}
# Set
unique_numbers = {1, 2, 3}
पायथन में टिप्पणियाँ (Comments) और डेटा प्रकार (Data Types) का विवरण
पायथन टिप्पणियाँ (Comments):
टिप्पणियाँ प्रोग्रामिंग में उपयोग की जाती हैं ताकि कोड के हिस्सों को समझाने के लिए या नोट्स जोड़ने के लिए कोड के अंदर टेक्स्ट को शामिल किया जा सके। पायथन में, टिप्पणियाँ कोड के निष्पादन को प्रभावित नहीं करती हैं और कोड के पढ़ने वालों के लिए उपयोगी होती हैं।
सिंगल-लाइन टिप्पणियाँ:
सिंगल-लाइन टिप्पणी को # प्रतीक का उपयोग करके लिखा जाता है।:
# यह एक सिंगल-लाइन टिप्पणी है
x = 5 # यह एक इनलाइन टिप्पणी है
मल्टी-लाइन टिप्पणियाँ:
पायथन में मल्टी-लाइन टिप्पणी को ट्रिपल क्वोट्स ''' या """ का उपयोग करके लिखा जा सकता है।
'''
यह एक
मल्टी-लाइन
टिप्पणी है
'''
"""
यह भी एक
मल्टी-लाइन
टिप्पणी है
"""
पायथन डेटा प्रकार (Data Types):
पायथन में डेटा प्रकार बहुत महत्वपूर्ण होते हैं क्योंकि वे वेरिएबल्स में स्टोर किए गए डेटा के प्रकार को निर्दिष्ट करते हैं। विभिन्न प्रकार के डेटा प्रकार पायथन में उपलब्ध हैं:
मूल डेटा प्रकार:
इंटीजर (Integer): पूर्णांक संख्या (जैसे, x = 10)
फ्लोट (Float): दशमलव संख्या (जैसे, y = 3.14)
स्ट्रिंग (String): अक्षरों का अनुक्रम (जैसे, name = "Alice")
बूलियन (Boolean): सत्य या असत्य मान (True या False, जैसे, is_active = True)
संग्रह डेटा प्रकार (Collection Data Types):
सूची (List): वस्तुओं का क्रमबद्ध संग्रह (जैसे, fruits = ["apple", "banana", "cherry"])
ट्यूपल (Tuple): क्रमबद्ध, अपरिवर्तनीय वस्तुओं का संग्रह (जैसे, coordinates = (10, 20))
डिक्शनरी (Dictionary): कुंजी-मूल्य जोड़े का संग्रह (जैसे, person = {"name": "Alice", "age": 25})
सेट (Set): अद्वितीय वस्तुओं का असंगठित संग्रह (जैसे, unique_numbers = {1, 2, 3})
उदाहरण उपयोग:
# इंटीजर
age = 25
# फ्लोट
price = 19.99
# स्ट्रिंग
name = "Alice"
# बूलियन
is_member = True
# सूची
fruits = ["apple", "banana", "cherry"]
# ट्यूपल
coordinates = (10, 20)
# डिक्शनरी
person = {"name": "Alice", "age": 25}
# सेट
unique_numbers = {1, 2, 3}
Python Identity Operators
Identity operators in Python are used to compare the memory locations of two objects. They are typically used to check if two variables point to the same object, not just if their values are the same.
The Two Identity Operators:
is Operator:
The is operator is used to test if two variables refer to the same object in memory.
It returns True if both variables point to the same object, otherwise, it returns False.
x = [1, 2, 3]
y = x
z = [1, 2, 3]
print(x is y) # Output: True (both refer to the same object)
print(x is z) # Output: False (different objects with the same value)
is not Operator:
The is not operator is the negation of the is operator.
It returns True if two variables do not refer to the same object, otherwise, it returns False.
x = [1, 2, 3]
y = x
z = [1, 2, 3]
print(x is not y) # Output: False (both refer to the same object)
print(x is not z) # Output: True (different objects with the same value)
Important Points to Note:
Identity vs. Equality:
Identity operators (is, is not) check if two variables point to the same object in memory.
Equality operators (==, !=) check if the values of two variables are equal, regardless of whether they are the same object.
x = [1, 2, 3]
y = [1, 2, 3]
print(x == y) # Output: True (values are equal)
print(x is y) # Output: False (different objects)
Immutable Types:
For immutable types like integers and strings, Python often reuses objects for small integers and interned strings.
This means that identity comparisons with is may sometimes return True for seemingly different variables.
a = 1000
b = 1000
print(a is b) # Output: False (different objects)
x = 5
y = 5
print(x is y) # Output: True (same object, due to integer caching)
Use Cases:
Identity operators are particularly useful when you need to confirm that two variables are referencing the exact same object, which is important in scenarios involving mutable objects (like lists, dictionaries) and singleton objects (like None).
Example Usage:
a = [1, 2, 3]
b = a
c = [1, 2, 3]
print(a is b) # True, because b references the same list as a
print(a is c) # False, because c is a different list with the same content
print(a == c) # True, because the contents of the lists are equal
x = "hello"
y = "hello"
z = "world"
print(x is y) # True, because Python reuses the same string object for 'hello'
print(x is z) # False, because 'hello' and 'world' are different string objects
Python पहचान ऑपरेटर
Python में पहचान ऑपरेटरों का उपयोग दो ऑब्जेक्ट्स के मेमोरी स्थानों की तुलना करने के लिए किया जाता है। इन्हें सामान्यतः यह जांचने के लिए उपयोग किया जाता है कि क्या दो वेरिएबल्स एक ही ऑब्जेक्ट को इंगित करते हैं, न कि केवल उनके मान समान हैं।
दो पहचान ऑपरेटर:
is ऑपरेटर:
is ऑपरेटर का उपयोग यह जांचने के लिए किया जाता है कि क्या दो वेरिएबल्स मेमोरी में एक ही ऑब्जेक्ट को संदर्भित करते हैं।
यदि दोनों वेरिएबल्स एक ही ऑब्जेक्ट को इंगित करते हैं तो यह True लौटाता है, अन्यथा False लौटाता है।
x = [1, 2, 3]
y = x
z = [1, 2, 3]
print(x is y) # Output: True (दोनों एक ही ऑब्जेक्ट को संदर्भित करते हैं)
print(x is z) # Output: False (समान मान वाले भिन्न ऑब्जेक्ट्स)
is not ऑपरेटर:
is not ऑपरेटर is ऑपरेटर का नकारात्मक रूप है।
यदि दो वेरिएबल्स एक ही ऑब्जेक्ट को संदर्भित नहीं करते हैं तो यह True लौटाता है, अन्यथा False लौटाता है।
x = [1, 2, 3]
y = x
z = [1, 2, 3]
print(x is not y) # Output: False (दोनों एक ही ऑब्जेक्ट को संदर्भित करते हैं)
print(x is not z) # Output: True (समान मान वाले भिन्न ऑब्जेक्ट्स)
महत्वपूर्ण बिंदु:
पहचान बनाम समानता:
पहचान ऑपरेटर (is, is not) यह जांचते हैं कि क्या दो वेरिएबल्स मेमोरी में एक ही ऑब्जेक्ट को इंगित करते हैं।
समानता ऑपरेटर (==, !=) यह जांचते हैं कि क्या दो वेरिएबल्स के मान समान हैं, भले ही वे एक ही ऑब्जेक्ट हों या नहीं।
x = [1, 2, 3]
y = [1, 2, 3]
print(x == y) # Output: True (मान समान हैं)
print(x is y) # Output: False (भिन्न ऑब्जेक्ट्स)
अपरिवर्तनीय प्रकार:
पूरे संख्या और स्ट्रिंग्स जैसे अपरिवर्तनीय प्रकारों के लिए, Python अक्सर छोटे पूर्णांकों और इंटरनल स्ट्रिंग्स के लिए ऑब्जेक्ट्स को पुन: उपयोग करता है।
इसका मतलब है कि पहचान तुलना is के साथ कभी-कभी True लौटा सकती है, जब कि वे भिन्न वेरिएबल्स प्रतीत होते हैं।
a = 1000
b = 1000
print(a is b) # Output: False (भिन्न ऑब्जेक्ट्स)
x = 5
y = 5
print(x is y) # Output: True (समान ऑब्जेक्ट, पूर्णांक कैशिंग के कारण)
उपयोग के मामले:
पहचान ऑपरेटर विशेष रूप से उपयोगी होते हैं जब आपको यह पुष्टि करने की आवश्यकता होती है कि दो वेरिएबल्स एक ही ऑब्जेक्ट को संदर्भित कर रहे हैं, जो कि परिवर्तनीय ऑब्जेक्ट्स (जैसे कि सूचियाँ, शब्दकोश) और एकलटन ऑब्जेक्ट्स (जैसे None) के मामलों में महत्वपूर्ण है।
उदाहरण:
a = [1, 2, 3]
b = a
c = [1, 2, 3]
print(a is b) # True, क्योंकि b वही सूची संदर्भित कर रहा है जो a को कर रहा है
print(a is c) # False, क्योंकि c एक भिन्न सूची है जिसकी सामग्री समान है
print(a == c) # True, क्योंकि सूचियों की सामग्री समान है
x = "hello"
y = "hello"
z = "world"
print(x is y) # True, क्योंकि Python 'hello' स्ट्रिंग ऑब्जेक्ट को पुन: उपयोग करता है
print(x is z) # False, क्योंकि 'hello' और 'world' भिन्न स्ट्रिंग ऑब्जेक्ट्स हैं
Assignment operators are used to assign a value or the result of an expression to a variable. Python has several types of assignment operators, which help in simplifying and shortening various tasks. Below is the information about different assignment operators:
General Assignment Operator (=):
This is the most common assignment operator, used to assign a value to a variable.
x = 5
Add and Assign Operator (+=):
This operator adds a value to the current value of the variable and then assigns the result to the same variable.
x = 5
x += 3 # x is now 8
Subtract and Assign Operator (-=):
This operator subtracts a value from the current value of the variable and then assigns the result to the same variable.
x = 5
x -= 2 # x is now 3
Multiply and Assign Operator (*=):
This operator multiplies the current value of the variable by a value and then assigns the result to the same variable.
x = 5
x *= 4 # x is now 20
Divide and Assign Operator (/=):
This operator divides the current value of the variable by a value and then assigns the result to the same variable.
x = 20
x /= 4 # x is now 5.0
Modulo and Assign Operator (%=):
This operator takes the modulo of the current value of the variable by a value and then assigns the result to the same variable.
x = 10
x %= 3 # x is now 1
Power and Assign Operator (**=):
This operator raises the current value of the variable to the power of a value and then assigns the result to the same variable.
x = 2
x **= 3 # x is now 8
Floor Division and Assign Operator (//=):
This operator performs floor division on the current value of the variable by a value and then assigns the result to the same variable.
x = 20
x //= 3 # x is now 6
Bitwise Assignment Operators:
AND Assignment (&=):
x = 5 # 0101
x &= 3 # 0011, x is now 1
OR Assignment (|=):
x = 5 # 0101
x |= 3 # 0011, x is now 7
XOR Assignment (^=):
pythonCopy codex = 5 # 0101
x ^= 3 # 0011, x is now 6
Shift Left Assignment (<<=):
x = 5 # 0101
x <<= 1 # x is now 10 (1010)
Shift Right Assignment (>>=):
pythonCopy codex = 5 # 0101
x >>= 1 # x is now 2 (0010)
असाइनमेंट ऑपरेटरों का उपयोग एक वेरिएबल को एक मान या अभिव्यक्ति का मान असाइन करने के लिए किया जाता है। Python में कई प्रकार के असाइनमेंट ऑपरेटर होते हैं, जो विभिन्न कार्यों को सरल और संक्षिप्त करने में मदद करते हैं। नीचे विभिन्न असाइनमेंट ऑपरेटरों के बारे में जानकारी दी गई है:
सामान्य असाइनमेंट ऑपरेटर (=):
यह सबसे सामान्य असाइनमेंट ऑपरेटर है, जिसका उपयोग एक वेरिएबल को एक मान असाइन करने के लिए किया जाता है।
x = 5
जोड़कर असाइनमेंट ऑपरेटर (+=):
यह ऑपरेटर वेरिएबल के वर्तमान मान में एक मान जोड़ता है और फिर परिणाम को उसी वेरिएबल को असाइन करता है।
x = 5
x += 3 # x अब 8 हो जाएगा
घटाकर असाइनमेंट ऑपरेटर (-=):
यह ऑपरेटर वेरिएबल के वर्तमान मान से एक मान घटाता है और फिर परिणाम को उसी वेरिएबल को असाइन करता है।
x = 5
x -= 2 # x अब 3 हो जाएगा
गुणा करके असाइनमेंट ऑपरेटर (*=):
यह ऑपरेटर वेरिएबल के वर्तमान मान को एक मान से गुणा करता है और फिर परिणाम को उसी वेरिएबल को असाइन करता है।
x = 5
x *= 4 # x अब 20 हो जाएगा
भाग देकर असाइनमेंट ऑपरेटर (/=):
यह ऑपरेटर वेरिएबल के वर्तमान मान को एक मान से भाग करता है और फिर परिणाम को उसी वेरिएबल को असाइन करता है।
x = 20
x /= 4 # x अब 5.0 हो जाएगा
Modulo असाइनमेंट ऑपरेटर (%=):
यह ऑपरेटर वेरिएबल के वर्तमान मान का माड्यूलो एक मान से निकालता है और फिर परिणाम को उसी वेरिएबल को असाइन करता है।
x = 10
x %= 3 # x अब 1 हो जाएगा
Power असाइनमेंट ऑपरेटर (**=):
यह ऑपरेटर वेरिएबल के वर्तमान मान को एक मान की शक्ति तक बढ़ाता है और फिर परिणाम को उसी वेरिएबल को असाइन करता है।
x = 2
x **= 3 # x अब 8 हो जाएगा
Floor Division असाइनमेंट ऑपरेटर (//=):
यह ऑपरेटर वेरिएबल के वर्तमान मान को एक मान से फर्श भाग करता है और फिर परिणाम को उसी वेरिएबल को असाइन करता है।
x = 20
x //= 3 # x अब 6 हो जाएगा
Bitwise Assignment Operators:
AND असाइनमेंट (&=):
x = 5 # 0101
x &= 3 # 0011, x अब 1 हो जाएगा
OR असाइनमेंट (|=):
x = 5 # 0101
x |= 3 # 0011, x अब 7 हो जाएगा
XOR असाइनमेंट (^=):
x = 5 # 0101
x ^= 3 # 0011, x अब 6 हो जाएगा
शिफ्ट लेफ्ट असाइनमेंट (<<=):
x = 5 # 0101
x <<= 1 # x अब 10 (1010) हो जाएगा
शिफ्ट राइट असाइनमेंट (>>=):
x = 5 # 0101
x >>= 1 # x अब 2 (0010) हो जाएगा
Comparison Operators in Python
Comparison operators in Python are used to compare two values. They return a boolean value indicating the result of the comparison: True if the comparison is true, and False if it is not. These operators are essential for decision-making processes in programming. Below is a description of the various comparison operators in Python:
Equal to (==):
The == operator checks if the values of two operands are equal.
x = 5
y = 5
print(x == y) # Output: True
Not equal to (!=):
The != operator checks if the values of two operands are not equal.
x = 5
y = 3
print(x != y) # Output: True
Greater than (>):
The > operator checks if the value of the left operand is greater than the value of the right operand.
x = 5
y = 3
print(x > y) # Output: True
Less than (<):
The < operator checks if the value of the left operand is less than the value of the right operand.
x = 5
y = 3
print(x < y) # Output: False
Greater than or equal to (>=):
The >= operator checks if the value of the left operand is greater than or equal to the value of the right operand.
x = 5
y = 5
print(x >= y) # Output: True
Less than or equal to (<=):
The <= operator checks if the value of the left operand is less than or equal to the value of the right operand.
x = 3
y = 5
print(x <= y) # Output: True
Examples
Using comparison operators in conditional statements:
Comparison operators are often used in if statements to make decisions based on certain conditions.
age = 18
if age >= 18:
print("You are an adult.")
else:
print("You are a minor.")
Using comparison operators in loops:
Comparison operators can be used in loops to control the execution flow.
numbers = [1, 2, 3, 4, 5]
for num in numbers:
if num > 3:
print(num)
Combining comparison operators:
Multiple comparison operators can be combined using logical operators (and, or, not) to form complex conditions.
x = 10
y = 20
z = 30
if x < y and y < z:
print("x is less than y and y is less than z")
Important Points
Type Comparison: Comparison operators can compare values of different types. However, the comparison might not always make sense (e.g., comparing a string with an integer).
print(5 == '5') # Output: False
Chained Comparisons: Python supports chained comparisons, which allow you to compare multiple values in a single statement.
x = 10
print(5 < x < 15) # Output: True
Comparing Sequences: Comparison operators can be used to compare sequences (like strings, lists, and tuples) lexicographically.
print('abc' < 'bcd') # Output: True
print([1, 2, 3] < [1, 2, 4]) # Output: True
Python Logical Operators
Logical operators in Python are used to combine conditional statements. They are essential in control flow statements like if, while, and for loops, enabling more complex decision-making processes. Python has three main logical operators: and, or, and not.
1. and Operator
The and operator returns True if both operands are true. If either operand is false, it returns False. It is used to ensure that multiple conditions are all true before executing a block of code.
Example:
x = 5
y = 10
if x > 0 and y > 5:
print("Both conditions are true") # Output: Both conditions are true
2. or Operator
The or operator returns True if at least one of the operands is true. If both operands are false, it returns False. It is used to check if at least one of multiple conditions is true.
Example:
x = 5
y = 3
if x > 0 or y > 5:
print("At least one condition is true") # Output: At least one condition is true
3. not Operator
The not operator returns True if the operand is false, and False if the operand is true. It is used to invert the boolean value of a condition.
Example:
x = 5
if not x > 10:
print("Condition is false") # Output: Condition is false
Combining Logical Operators
Logical operators can be combined to form more complex conditions. The operations are evaluated in the order of not, and, and then or.
Example:
x = 5
y = 10
z = 15
if x > 0 and y > 5 or z < 20:
print("Complex condition is true") # Output: Complex condition is true
Operator Precedence
Python evaluates logical operators based on precedence:
not
and
or
Example:
x = 5
y = 10
z = 15
if not x > 0 or y > 5 and z < 20:
print("Operator precedence is followed") # Output: Operator precedence is followed
In this example, not x > 0 is evaluated first, then y > 5 and z < 20, and finally the or operator combines the results.
Practical Examples
Logical operators are frequently used in control flow to execute code based on multiple conditions.
Example:
age = 25
income = 50000
if age > 18 and income > 40000:
print("Eligible for loan") # Output: Eligible for loan
else:
print("Not eligible for loan")
In this example, the and operator ensures that the person is eligible for a loan only if both conditions are true.
Example:
is_student = True
is_employed = False
if is_student or is_employed:
print("Eligible for discount") # Output: Eligible for discount
else:
print("Not eligible for discount")
Here, the or operator ensures that a person is eligible for a discount if either condition is true.
Python Membership Operators
Membership operators in Python are used to test whether a value or variable is found in a sequence (such as a string, list, tuple, set, or dictionary). Python has two membership operators: in and not in.
1. in Operator
The in operator is used to check if a value exists within a sequence. If the value is found in the sequence, it returns True, otherwise, it returns False.
Example:
# String example
string = "hello"
print('h' in string) # Output: True
# List example
lst = [1, 2, 3, 4, 5]
print(3 in lst) # Output: True
# Dictionary example
dict = {'a': 1, 'b': 2, 'c': 3}
print('a' in dict) # Output: True (checks keys by default)
2. not in Operator
The not in operator is used to check if a value does not exist within a sequence. If the value is not found in the sequence, it returns True, otherwise, it returns False.
Example:
# String example
string = "hello"
print('z' not in string) # Output: True
# List example
lst = [1, 2, 3, 4, 5]
print(6 not in lst) # Output: True
# Dictionary example
dict = {'a': 1, 'b': 2, 'c': 3}
print('d' not in dict) # Output: True (checks keys by default)
Practical Examples
Membership operators are often used in control flow statements to determine if an element is present or absent in a collection.
Example 1: List
fruits = ["apple", "banana", "cherry"]
if "banana" in fruits:
print("Banana is in the list") # Output: Banana is in the list
else:
print("Banana is not in the list")
Example 2: Loops
numbers = [1, 2, 3, 4, 5]
for number in numbers:
if number in [2, 4]:
print(f"{number} is even")
else:
print(f"{number} is odd")
Output:
1 is odd
2 is even
3 is odd
4 is even
5 is odd
Example 3: String
sentence = "The quick brown fox"
if "quick" in sentence:
print("The word 'quick' is in the sentence") # Output: The word 'quick' is in the sentence
else:
print("The word 'quick' is not in the sentence")
Example 4: Dictionaries
When using membership operators with dictionaries, they check for the presence of keys, not values.
Example:
son = {"name": "Alice", "age": 25, "city": "New York"}
# Check if a key exists
if "name" in person:
print("Name key is present") # Output: Name key is present
# Check if a value exists (requires explicit check)
if 25 in person.values():
print("Age 25 is present") # Output: Age 25 is present
Example 5: Sets
Sets are collections of unique elements, and membership operators can be used to check the presence of elements efficiently.
Example:
my_set = {1, 2, 3, 4, 5}
print(3 in my_set) # Output: True
print(6 not in my_set) # Output: True
Summary
in operator: Returns True if a value is found in the sequence.
not in operator: Returns True if a value is not found in the sequence.
Unlock the power of Python programming with our introductory course designed for absolute beginners. Whether you're aiming to build a career in data science, web development, or automation, mastering Python is the first step. In this course, we will use Google Colab, a cloud-based environment, to make learning seamless and accessible.
What You'll Learn:
Introduction to Google Colab:
Setting up and navigating Google Colab for Python programming.
Benefits of using Google Colab for beginners.
Understanding IDE Basics:
Overview of Integrated Development Environments (IDEs).
Installing and setting up popular Python IDEs.
Comparing Google Colab with traditional IDEs.
Python Data Types:
Introduction to different data types in Python (integers, floats, strings, lists, tuples, dictionaries, sets).
Practical examples and exercises to understand the use of each data type.
Using Comments in Python:
Importance of comments in code.
How to write single-line and multi-line comments.
Working with Variables:
Understanding variables and their role in Python programming.
Rules for naming variables.
Assigning values to variables and working with multiple variables.
Operators in Python:
Exploring different types of operators (arithmetic, comparison, logical, bitwise, assignment, membership, identity).
Practical examples to understand how each operator works.
Who Should Enroll:
Aspiring programmers with no prior coding experience.
Students and professionals looking to enhance their skills in Python.
Anyone interested in learning programming for data science, web development, automation, and more.