Python Programming Fundamentals: String and List Manipulation, Study Guides, Projects, Research of Information Technology

A concise overview of fundamental programming concepts in python, focusing on string and list manipulation techniques. It covers essential topics such as slice notation, field width, alignment, fill characters, and floating-point precision for strings. Additionally, it details various string methods like replace, find, count, and isalnum, as well as list operations including appending, extending, inserting, removing, and sorting. The document also touches on list comprehensions and dictionary implementations, offering a comprehensive guide for beginners to grasp basic python programming skills. It serves as a quick reference for students learning python, summarizing key methods and operations with examples. Structured to aid quick recall and understanding of python's string and list functionalities, making it a valuable resource for coding exercises and assignments.

Typology: Study Guides, Projects, Research

2024/2025

Available from 08/06/2025

DOCSGRADER001
DOCSGRADER001 🇺🇸

4.7

(9)

4K documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1 /
5
C 859: Introduction to Programming in Python
1.Slice Notation: my_str[start:end]
will create a substring from my_str index start through to end - 1
2.Slice Notation with Stride: my_str[start:end:stride]
3.Field Width: minimum number of characters that must be inserted into a string my_str='{name:16}{goals:8}'
4.Alignment Character: determines how a value should be aligned within the width of the field
my_str='[name:<16}{goals:>8}{points:^6}'
5.Fill Character: used to pad a replacement field when the string being inserted is smaller than the field width
{score:0>4} produces 0018
6.Floating-point Precision: format specification which indicates how many digits to the right of the decimal should
be included in the output of floating types '{:.1f}'.format(1.725) produces 1.7
7.Replace (old and new parameters): returns a copy of the string with all occur- rences of the substring old replaced
by the string new; old and new arguments may be string variables or string literals
phrase.replace('one', 'two') will replace any occurrence of the word one with two
8.Replace (old, new and count parameters): returns a copy of the string with all occurences of the substring old
replaced by the new except only replaces the first count occurrences of old
phrase.replace('one', 'two', 5) will replace any occurrances of 'one' with 'two' but will begin looking at index 5
9.Find (with x variable): string method that returns the index of the first occurrence of item x in the string, else returns
-1
my_str.find('!') will return the index of the '!' in the string
pf3
pf4
pf5

Partial preview of the text

Download Python Programming Fundamentals: String and List Manipulation and more Study Guides, Projects, Research Information Technology in PDF only on Docsity!

1 /

C 859: Introduction to Programming in Python

1. Slice Notation: my_str[start:end]

will create a substring from my_str index start through to end - 1

2. Slice Notation with Stride: my_str[start:end:stride]

3. Field Width: minimum number of characters that must be inserted into a string my_str='{name:16}{goals:8}'

4. Alignment Character: determines how a value should be aligned within the width of the field

my_str='[name:<16}{goals:>8}{points:^6}'

5. Fill Character: used to pad a replacement field when the string being inserted is smaller than the field width

{score:0>4} produces 0018

6. Floating-point Precision: format specification which indicates how many digits to the right of the decimal should

be included in the output of floating types '{:.1f}'.format(1.725) produces 1.

7. Replace (old and new parameters): returns a copy of the string with all occur- rences of the substring old replaced

by the string new; old and new arguments may be string variables or string literals phrase.replace('one', 'two') will replace any occurrence of the word one with two

8. Replace (old, new and count parameters): returns a copy of the string with all occurences of the substring old

replaced by the new except only replaces the first count occurrences of old phrase.replace('one', 'two', 5) will replace any occurrances of 'one' with 'two' but will begin looking at index 5

9. Find (with x variable): string method that returns the index of the first occurrence of item x in the string, else returns

my_str.find('!') will return the index of the '!' in the string

2 /

10. Find (with x, and start parameter): string method that will return the index of the first occurrence of item x in

the string but will begin looking at index start my_str.find('!', 3) will produce the index of the first! on or after index 3

11. Find (with x, start and end parameter): string method that will return the index of the first occurrence of item x

in the string but will begin looking at the index start and will stop at index end - my_str.find('!', 3, 8) will produce the index of the first! but will start looking at index 3 and stop looking at index 8 - 1

12. Rfind: string method that works the same as str.find(x) but looks for the para- meter in reverse, returning the last

occurrence in the string my_str.rfind('!') will give the index of the last! in the string

13. Count: string method that returns the number of times x occurs in the string my_str.count('oo') will return the

number of times 'oo' occurs in the string

14. isalnum(): string method that returns true if all characters in the string are lowercase or uppercase letters or

the numbers 0-

15. isdigit(): string method that returns true if all characters are the numbers 0- 9

16. islower(): string method that returns true if all cased characters are lowercase letters

17. isupper(): string method that return true if all cased characters are uppercase letters

18. isspace(): string method that returns true if all characters are whitespace

19. startswith(x): string method that that returns true if the string starts with x

20. endswith(x): string method that returns true if the string ends with x

21. capitalize(): string method that returns a copy of the string with the first charac- ter capitalized and the rest

lowercased

22. lower(): string method that returns a copy of the string with all characters lowercased

4 /

39. list.remove(x): list method that Remove first item from list with value x

40. list.pop(): list method that remove and return element at end of the list

41. list.pop(i): list method that removes and returns item at position i in list

42. list.sort(): list method that sort the items of list in-place

43. list.reverse(): list method that reverse the elements of the list, in place

44. list.index(x): list method that return index of first item in list with value x

45. list.count(x): list method that return the number of times x appears in the list

46. enumerate(): list function that iterates over a list and provides and iteration counter

47. all(list): True if every element in list is True (!= 0), or if the list is empty

48. any(list): True if any element in the list is True

49. max(list): Find the element in list with the largest value

50. min(list): Get the minimum element in the list

51. sum(list): Get the sum of all elements in the list

52. my_list = [[5, 13], [50, 75, 100]]: list nesting: creates a list as an object of a list

53. mylist[start: end]: slice notation for list; will create a new list with items at start to end - 1

54. mylist[start:end:stride]: get a list of every stride element from start to end - 1

55. mylist[start:]: get a list from start to end of the list

56. mylist[:end]: get a list from beginning of list to end - 1

57. mylist[:]: gets a copy of a list

5 /

58. new_list=[expression for loop_var in list]: do the expression for each item in the list

List Comprehension

59. new_list=[expression for item in list if condition]: do the expression for each item in the list if a condition is true

Conditional List Comprehension

60. mylist.sort(): sort items of a list in ascending order; in-place modification

61. mynewlist = sorted(mylist): sorts items of a list in ascending order and creates a new list

62. mynewlist = sorted(mylist, key=str.lower): this will put all items in a list to lowercase before sorting them to

ensure that a will come before B

63. mynewlist = sorted(mylist, reverse=True): create a new list sorted in reverse

64. dict(): implements a dictionary

dict(Bobby='805-555-2232', Johnny='951-555-0055') dict([('Bobby', '805-555-2232'), ('Johnny', '951-555-0055')])

65. my_dict[key]: Indexing operation - retrieves the value associated with key jose_grade = my_dict['Jose']

66. my_dict[key] = value: adds an entry if the entry does not exit, else modifies the existing entry

my_dict['Jose'] = 'B+'

67. del my_dict[key]: Deletes the key from a dictionary del my_dict['Jose']

68. key in my_dict: test for existence of key in my_dict if 'Jose' in

my_dict: ......