Chapter 2: Variables and Simple Data Types Flashcards
(33 cards)
Example of creating a variable in python
message = “Hello Python Crash Course reader”
Ayhting inside ____ is considered a _____ in python
quotes, string
‘This is a string’
is this a string in python?
yes
name = “ada lovelace”
print(name.title())
Ada Lovelace
Other string case methods
.upper()
.lower()
first_name = “ada”
last_name = “lovelace”
full_name = f”{first_name} {last_name}”
print(full_name)
ada lovelace
These strings are called f-strings
full_name = “{} {}”.format(first_name, last_name)
f strings are only in python 3.6, so this is the method you would have to use if using earlier versions. version
In python, you can also add white space to strings with ____ ro ____
\t, \n
.rstrip()
removes whitespace from the right side
.lstrip()
removes whitespace from the left hand side
message = ‘One of Python’s strengths is its diverse community.’
print(message)
File “apostrophe.py”, line 1
message = ‘One of Python’s strengths is its diverse community.’
^
SyntaxError: invalid syntax
Python uses ____ multiplication symbols to represent exponents
two
> > 3**2
9
When dividing any two numbers, even if they are integers that result in a whol number, you will always get a ______
float
> > > universe_age = 14_000_000_000
print(universe_age)
14000000000
x,y,z = 0,0,0
multiple assignment
When you want to treat a variable as a constant in your code, make the
name of the variable all _____ letters.
capital
MAX_CONNECTIONS = 5000
How to make a string from another data type
str()
»> str(98.6)
‘98.6’
How to combine string variables
+
How to duplicate a string
> > > start = ‘Na ‘ * 4 + ‘\n’
How to get an individual character in string
[]
»> letters = ‘abcdefghijklmnopqrstuvwxyz’
»> letters[0]
‘a’
How to get a substring
using a slice
[:]
extracts the entire sequence from start to end.
[ start :]
specifies from the start offset to the end.