លំហសិក្សាកម្ពុជា លំហសិក្សាកម្ពុជា V1.0
ចូល ចុះឈ្មោះ

Python Programming: Variables, Basic Operations and String manipulate

ពាក្យគន្លឹះ

Python Programming: Variables, Basic Operations and String manipulate


In programming, a variable is a named storage location in memory that holds data

(value) which can be manipulated during program execution. Key features include:

Name: Uniquely identifies the variable.

Value: Stores data of various types (e.g., integer, string).

Memory Location: Points to a specific memory address.

Type: Defines the kind of data stored, either explicitly declared or inferred.

Scope: Determines where the variable can be accessed (e.g., local, global).

Mutability: Indicates whether the value can be changed after assignment.

Variables are essential for storing and managing data in programming

+In programming languages like Python, the process of creating a

variable involves assigning a value to a name using the

assignment operator (=).

+In Python, variables store and reference data. Python is dynamically typed, so variable

types are inferred without explicit declaration

▪ Whole numbers without decimal points.

Example: x = 10

+Here are common variable types in Python:

Booleans (bool):

▪ Represents either True or False.

▪ Used in logical operations and conditional statements.

Example: is_valid = True

+Here are common variable types in Python:

Dictionaries (dict):

▪ Unordered collection of key-value pairs.

Example: person = {'name': 'Alice', 'age': 25}

+• It's important to note that variables in Python are case-sensitive (myVar is different from

myvar).

Additionally, variable names cannot start with a number and should follow the conventions

outlined in PEP 8 for code style.

+In programming, basic operations refer to the fundamental actions you can perform

on variables and values. These operations are common across many programming

languages and include arithmetic, assignment, comparison, logical, and Membership

operations.

+Here's an overview of some basic operations:

Arithmetic Operations:

Division ( / ): Divides the left operand by the right operand.

Example: result = 15 / 3 # result is now 5.0 (floating-point division)

Floor Division ( // ): Divides and rounds down to the nearest integer.

Example: result = 17 // 5 # result is now 3 (integer division)

+Here's an overview of some basic operations:

Assignment Operation:

Assignment (=): Assigns a value to a variable.

Example: x = 10

In-Place Operations (+=, -=, *=, /=): Perform an operation and

update the variable in-place.

Example: y = 5

y += 3 # Equivalent to y = y + 3, y is now 8

+Here's an overview of some basic operations:

Comparison Operations:

Equality (==): Checks if two values are equal.

Example: result = (3 == 3) # result is True

+Here's an overview of some basic operations:

Logical Operations:

Logical AND (and): Returns True if both conditions are true.

Example: result = (True and False) # result is False

+Here's an overview of some basic operations:

Membership Operations:

Membership (in): Returns True if a value is found in the sequence.

Example: result = (2 in [1, 2, 3]) # result is True

Not in (not in): Returns True if a value is not found in the sequence.

Example: result = (4 not in [1, 2, 3]) # result is True

+String manipulation is a common and essential task in programming. It involves working with strings, which are

sequences of characters, to modify, analyze, and manipulate the text.

Here are some common string manipulation operations in Python:

Concatenation:

Joining two or more strings together.

Example: str1 = "Hello"

str2 = "World"

result = str1 + " " + str2

result1 = (str1, str2)

print(result) # result is "Hello World"

print(result1) # result is (“Hello”, “World”)

+3. Length of a String:

Determining the number of characters in a string.

Example: text = "Python"

length = len(text)

print(length) # length is 6

4. Accessing Individual Characters:

Retrieving characters at specific positions.

Example: text = "Python"

first_char = text[0]

print(first_char) # first_char is 'P’

+Here are some common string manipulation operations in Python:

Conversion:

Converting between uppercase and lowercase.

Example: text = "Hello"

uppercase_text = text.upper()

print(uppercase_text) # uppercase_text is "HELLO"

lowercase_text = text.lower()

print(lowercase_text) # lowercase_text is "hello"

+Here are some common string manipulation operations in Python:

9. Checking for Substrings:

Verifying if a substring is present in a string.

Example: text = "Python is fun"

is_fun_present = "fun" in text

print(is_fun_present) # is_fun_present is True

10. Splitting:

Breaking a string into a list of substrings based on a delimiter.

Example: sentence = "This is a sentence."

words = sentence.split()

print(words) # words is ['This', 'is', 'a', 'sentence.’]

Python offers powerful string manipulation methods, enabling versatile text handling for

various applications. Understanding these methods is key to achieving desired results

efficiently

 

ចែករំលែក
រក្សាទុក
ការផ្សព្វផ្សាយ
ការផ្សព្វផ្សាយ
មតិយោបល់ 0
ចូលគណនី ដើម្បីបញ្ចេញមតិ