Python Syntax - Beginner Flashcards
Symbol used to render following symbol exactly.
\
Ex. To get a quotation mark in a string you would use: (“This guy said"Go" so I went.”)
Called the: escape character.
How do you insert a new line into a string?
\n
How do you concatenate a string?
+
Ex.
Print (“dogs” + “ rule”)
.upper & .isupper
.upper capitalizes a string
.isupper Checks to see if string has caps. (Checks original value, you can print in all caps with .upper without changing original value of variable.
Function that counts letters in a string.
len
Ex. print(len(variableA))
How do you extract a single character from a string?
[ index value goes here ]
Ex. String = Me
To get M
print(variableName [0])
How do you see where in the index a character is located in a string?
print(variable.index(“a”))
If more than one letter in a word, only returns index for first one.
How do you replace characters in a string?
variable.replace(“string to be replaced”, “new string that will replace it)
How does order of operations work in python?
Parenthesis work like typical.
What is this and what does it do? Print(10 %3)
Modulus operator. Divides first number by second number and gives you remember.
Say it 10 mod three, good for finding even numbers.
How do you turn a number into a string?
Print(str(variable or number))
Allows you to print numbers and strings together, otherwise they do not concatenate.
How do you get the absolutely value of a number?
abs
Ex.
print(abs(num_variable))
What does the function pow(2, 4) do?
Raises 2 to the fourth power.
How do you find the highest value number in a set?
max
Ex. print(max(4,6,2))
Returns 6.
What does the min function do?
Prints the smallest number.
What is the function for rounding a number?
round - follows normal rounding rules.
How do you import the math library in python?
from math import *
What does print(floor(3.7) do?
Always rounds down
What function do you use to always round up?
Ceil
Ex.
print(ceil(3.2) returns 4.
What is the function to return a square root?
Sqrt()
How do you allow the user to give the program information?
Use function input(prompt for user)
What is the default data type for user input in python?
String
How do you set user input to a number?
Add int (variable) or float(variable)
Syntax for creating a variable that is a list?
variable_name = [value1, “item2”, “item3”
Can python store different data types in the same list?
Yes.
Syntax to call a specific item from list if index value is known?
Ex. print(friends[index value])