Chapter 4 - Data types Flashcards
What is data?
information - the quantities, characters or symbols that operations are performed on by a computer, and which may also be stored and transmitted
What is an intiger?
Whole numbers both positive and negative ie 3
usually 1-4 bytes
What is a float AKA real numbers
All real numbers - decimals and integers
usually 4-10 bytes
What is a character
A single character
What is a string?
Multiple characters ( a list of characters ). A char can be a string, but a ( multiple - character ) string cannot be a char.
Typically 1 byte per character
What is boolean?
true or false
What does a strongly typed language mean?
You must explicitly state the data type when creating the variable ie C or C++
What does a weakly-typed language mean?
The programming language “guesses” what data type should be used ie python
Fix this code (its python):
height A = input(‘Enter heightA’)
height B = input(‘Enter heightB’)
totalHeight = heightA + heightB
print(totalHeight)
height A = input(‘Enter heightA’)
height B = input(‘Enter heightB’)
totalHeight = int(heightA) + int(heightB)
print(totalHeight)
What does len(string) do?
counts number of characters
What does string.capitalize() do?
capitlises the first letter
What does string.lower() do?
Puts the whole string in lower case
What does string.upper() do?
Puts the whole string in upper case
What does string.rjust() do?
sets how far across the page the string is. ie “dog”.rjust(50)
= dog
What does string.replace(oldvalue,newcalue) do?
replaces a value with a new value
What does string.split(separator) do?
Splits the string at a point
What does string.strip(characters) do?
Gets rid of characters stated
What does string.join(list) do?
makes it into a list
What does string.find(substring) do?
used to find the first occurrence of a sub-string in the specified string being called upon.
What does string.format(items) do?
he {0} in the format string is a format item. 0 is the index of the object whose string value will be inserted at that position.
What does \n do?
Newline
What does \t do?
tab
What does " do?
prints quote mark
What does \ do?
print backslash
demoStr = “Mr Reed is an awesome teacher!”
demoStr[3:9]
Reed i
demoStr = “Mr Reed is an awesome teacher!”
demoStr[-8:-1]
teacher
demoStr = “Mr Reed is an awesome teacher!”
demoStr[:3] + demoStr[22:]
Reed teacher!
Combining strings is called what?
Concatenation
What can you use index positions to perform?
String traversals iterating through a string to perform tasks
Can you give an example code of string traversals
demoStr = “Mr Reed is an awesome teacher!”
for char in demoStr:
if char == “a” or char == “e”
print(“Bingo!”)
Since python starts counting index positions from 0, the first character in a string has position …… , meaning that the length of a string is ……….. than the index of it’s final character.
zero
one more