1/10
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
What is indexing? Explain it and how it’s written and alternatives
Indexing = Allowing us to access elements of a sequence with [ ] (indexing operator)
[start : end : step]
[1]
[:2:]
[1::]
[::9]
Find the first number in Credit_number = “1234 - 5687- 90123 - 4567”
print(Credit_number [0])
returns 1
Write to find first four digits of Credit_number = “1234 - 5687- 90123 - 4567”
print(Credit_number [0:4])
This gives us start and stop, so first four elements
This will have us start start at one, and end 4
Indexing is exclusive, so if I see 4, it won't include that one. It will go up to 3)
I can also write this as (:4) because I don’t need the zero
Write to find 5th to 9th digits of Credit_number = “1234 - 5687- 90123 - 4567”
print(Credit_number [5:9])
This will print 5678
(because dash counts as one)
Write to find all numbers up to 5th place of Credit_number = “1234 - 5687- 90123 - 4567”
print(Credit_number [:5])
This will print everything up to the 5th place of a string
However, remeber that the first of these 5 is labeled as zero.
Returns 1234-
Explain the inclusive / exclusive nature of indexing and slicing.
[start : end : step]
Start is inclusive
End is exclusive
if end is 5, it will count up to 4 places in the srring.
Write to find all numbers AFTER the 5th place of Credit_number = “1234 - 5687- 90123 - 4567”
print(Credit_number[5:]
5687- 90123 - 4567
Return the last number of the credit card number and explain why
print(Credit_number [-1])
Return the 3rd to last number of the credit card number and explain why
print(Credit_number [-3])
Write the credit card number backwards and explain why
print(Credit_number [::-1])
Write a script to show the last for like this: xxxx-xxxx-xxxx-4567
Credit_number = “1234 - 5687- 90123 - 4567”
Credit_number = Credit_number[-4:]
print(f”xxxx-xxxx-xxxx-{Credit_number}”)