Data Casting, Variables & Strings
Data Casting, Variables & Strings
In Python, primitive data types are also known as built-in data types or fundamental data types.
These are the basic data types that represent single values. Python has several primitive data
types, including:
python
x = 5
python
y = 3.14
String (str): Represents sequences of characters, enclosed in either single, double, or triple
quotes. For example:
python
name = "Alice"
Boolean (bool): Represents the binary values True or False, used for logical operations. For
example:
python
is_valid = True
NoneType (None): Represents the absence of a value or a null value. For example:
python
value = None
Complex (complex): Represents complex numbers with real and imaginary parts. For example:
python
z = 2 + 3j
These primitive data types are the building blocks for more complex data structures and can be
used to store and manipulate data in Python programs. Python is dynamically typed, which
means you don't need to declare the data type explicitly; it is inferred based on the value assigned
to a variable.
In addition to these built-in data types, Python also provides various data structures like lists,
tuples, sets, and dictionaries that can hold multiple values of different types. These data
structures are often used to work with collections of data.
Defining Variables
1. Names must start with a letter or the underscore (_) character, and the remaining characters
must be letters, numbers, or underscores.
2. You cannot use other symbols such as ? or %. Spaces are not permitted inside names either.
You can use uppercase letters to denote word boundaries, as in cansPerPack. This naming
convention is called camel case because the uppercase letters in the middle of the name look like
the humps of a camel.
3. Names are case sensitive, that is, canVolume and canvolume are different names.
4. You cannot use reserved words such as if or class as names; these words are reserved
exclusively for their special Python meanings. (See Appendix B for a listing of all reserved
words in Python.)
These are firm rules of the Python language. There are two “rules of good taste” that you
should also respect.
1. It is better to use a descriptive name, such as cansPerPack, than a terse name, such
as cpp.
2. Most Python programmers use names for variables that start with a lowercase
letter (such as cansPerPack). In contrast, names that are all uppercase (such as CAN_VOLUME)
indicate constants. Names that start with an uppercase letter are commonly used for user-defined
data types (such as GraphicsWindow).
Comments
As your programs get more complex, you should add comments, explanations for human readers
of your code. For example, here is a comment that explains the value used in a constant:
Python uses the exponential operator ** to denote the power operation. For example, the
Python equivalent of the mathematical expression a2 is a ** 2.
Strings
In Python, a string is a sequence of characters enclosed within single (' '), double (" "), or triple
(''' ' ''' or """ """ for multiline strings) quotation marks. Strings are one of the fundamental data
types in Python and are widely used for storing and manipulating textual data.
Here are some basic operations and concepts related to strings in Python:
Creating Strings: You can create strings by enclosing text within single or double quotation
marks:
python
single_quoted_string = 'This is a single-quoted string.'
double_quoted_string = "This is a double-quoted string."
python
multiline_string = '''This is a
multiline
string.'''
String Concatenation: You can concatenate (join) strings using the + operator:
python
first_name = "John"
last_name = "Doe"
full_name = first_name + " " + last_name
String Length: You can find the length of a string using the len() function:
python
text = "Hello, World!"
length = len(text) # Length is 13
python
text = "Hello"
first_char = text[0] # 'H'
You can also use negative indexing to access characters from the end of the string:
python
last_char = text[-1] # 'o'
python
text = "Hello, World!"
slice = text[0:5] # 'Hello'
String Methods: Python provides many built-in string methods for manipulating strings,
such as upper(), lower(), strip(), split(), and replace(). Here are some examples:
python
text = " Python Programming "
stripped_text = text.strip() # Removes leading and trailing spaces
uppercase_text = text.upper() # Converts to uppercase
lowercase_text = text.lower() # Converts to lowercase
words = text.split() # Splits into a list of words
replaced_text = text.replace("Python", "Java") # Replaces text
String Formatting: You can format strings using f-strings (formatted string literals) or the
format() method:
python
name = "Alice"
age = 30
formatted_string = f"My name is {name} and I am {age} years old."
String Escape Sequences: Escape sequences allow you to include special characters within
strings, such as newline (\n), tab (\t), and others. For example:
python
escaped_string = "This is a\nmultiline\nstring."
String Immutability: Strings in Python are immutable, meaning you cannot change
individual characters of a string once it's created. You can create a new string with the desired
modifications.
python
original_string = "Hello"
modified_string = original_string + " World" # Creates a new string