Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Articles by Mohd Mohtashim
Page 5 of 19
Updating Strings in Python
You can "update" an existing string by (re)assigning a variable to another string. The new value can be related to its previous value or to a completely different string altogether. String Reassignment Since strings are immutable in Python, you cannot modify them directly. Instead, you create a new string ? var1 = 'Hello World!' var1 = 'Hello Python!' print("Updated String:", var1) Updated String: Hello Python! Combining Parts of Original String You can create a new string by combining parts of the original string with new content ? var1 ...
Read MoreAccessing Values of Strings in Python
Python does not support a character type; these are treated as strings of length one, thus also considered a substring. To access individual characters or substrings, use square brackets with the index or slice notation. Accessing Individual Characters Use square brackets with an index to access a single character. Python uses zero-based indexing ? var1 = 'Hello World!' var2 = "Python Programming" print("var1[0]:", var1[0]) print("var1[6]:", var1[6]) print("var2[0]:", var2[0]) print("var2[-1]:", var2[-1]) # Last character var1[0]: H var1[6]: W var2[0]: P var2[-1]: g Accessing Substrings with Slicing Use slice notation ...
Read MoreLoop Control Statements in Python
Loop control statements in Python allow you to change the execution flow of loops. These statements provide flexibility to terminate loops early, skip iterations, or act as placeholders in your code structure. The break Statement The break statement terminates the loop immediately and transfers control to the statement following the loop ? # Using break in a for loop numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] for num in numbers: if num == 5: print(f"Found {num}, breaking the ...
Read MoreData Type Conversion in Python
Data type conversion in Python allows you to transform one data type into another. Python provides built-in functions to perform these conversions, which return new objects representing the converted values. Common Type Conversion Functions Function Description Example int() Converts to integer int("42") → 42 float() Converts to floating-point float("3.14") → 3.14 str() Converts to string str(42) → "42" bool() Converts to boolean bool(1) → True list() Converts to list list("abc") → ['a', 'b', 'c'] tuple() Converts to tuple tuple([1, 2, 3]) → (1, 2, ...
Read MoreString Data Type in Python
Strings in Python are identified as a contiguous set of characters represented in quotation marks. Python allows for either pairs of single or double quotes. Subsets of strings can be taken using the slice operator [ ] and [:] with indexes starting at 0 in the beginning of the string and working their way from -1 at the end. String Creation You can create strings using single quotes, double quotes, or triple quotes for multi-line strings ? # Different ways to create strings single_quote = 'Hello World!' double_quote = "Hello World!" multi_line = """This is a ...
Read MoreNumber Data Type in Python
Number data types store numeric values. Number objects are created when you assign a value to them. For example − var1 = 1 var2 = 10 print(var1, var2) 1 10 You can also delete the reference to a number object by using the del statement. The syntax of the del statement is − del var1[, var2[, var3[...., varN]]]] You can delete a single object or multiple objects by using the del statement. For example − number1 = 42 number2 = 3.14 # Delete single object del ...
Read MoreMultiple Statements in Python
Python allows you to write multiple statements on a single line or group them into code blocks called suites. This flexibility helps organize your code efficiently. Multiple Statements on a Single Line The semicolon (;) allows multiple statements on the same line, provided that neither statement starts a new code block ? import sys; x = 'foo'; sys.stdout.write(x + '') foo Example with Variables You can assign multiple variables and perform operations on a single line ? a = 5; b = 10; result = a + b; ...
Read MoreQuotation in Python
Python accepts single ('), double (") and triple (''' or """) quotes to denote string literals, as long as the same type of quote starts and ends the string. Types of Quotes in Python Python supports three types of quotation marks for creating strings. Each has its specific use cases and advantages. Single Quotes Single quotes are the most basic way to create strings in Python ? word = 'Hello' name = 'Python' print(word) print(name) Hello Python Double Quotes Double quotes work exactly like single quotes but are ...
Read MoreLines and Indentation in Python
Python uses indentation instead of braces to define blocks of code. Unlike languages such as C++ or Java, Python relies on consistent spacing to group statements together for functions, classes, and control structures. Basic Indentation Rules All statements within a block must be indented with the same number of spaces. The Python convention is to use 4 spaces per indentation level ? if True: print("True") else: print("False") True Inconsistent Indentation Error Python will raise an IndentationError when statements in the same block ...
Read MoreWhat are Python Identifiers?
A Python identifier is a name used to identify a variable, function, class, module or other object. An identifier starts with a letter A to Z or a to z or an underscore (_) followed by zero or more letters, underscores and digits (0 to 9). Python does not allow punctuation characters such as @, $, and % within identifiers. Python is a case sensitive programming language. Thus, Manpower and manpower are two different identifiers in Python. Rules for Python Identifiers Python identifiers must follow these rules ? Must start with a letter (a-z, A-Z) ...
Read More