Www.abss.k12.nc.us



Unit 1 – Python language basics, introducing data types, variables, input, functions, operators, conditional statements, loops, and incrementing.Module 1 – Python BasicsComments are for humans to read. Computers will NOT execute these statements.Strings are sets of characters. In Python, strings are in double or single quotes like “Hello, world!” or ‘Hello, world!’a sequence of characters (aka: a string of characters): A, B, C, a, b, c, 1, 2, 3, ! @ #, &)is contained in quotes, single (‘) or double (“)The quotes containing a string must come in matched pairs: “hello” or ‘hello’ are correct, but “hello’ or ‘hello” are incorrectcan contain any character, this includes letters, spaces, punctuation, number digits and formatting. In a string with digits (“123”), the digits are text images rather than representations of numeric values.Integers are another Data TypeWhole numbers such as -2, -1, 0,1, 2, 3 are integersIntegers are NOT placed in quotesVariables are named containers. It can be addressed by name to access the contents of the container. Descriptive names reduce the need for comments in the code and make it easier to share code or read code written long ago.Use descriptive namesMust be camelCase (first word is all lower case and each word after begins with a capital letter)Must begin with a letter? (valid examples: itemPrice, studentName, stockSymbol, address, num1)Must only contain letters, numbers or _Not contain spaces or dashes, punctuation, quotation marks and other special charactersAre case sensitive (TAX, Tax, tax – are 3 different variables)Variables ?can be initialized with different data typesVariables can hold strings. Once a variable is assigned, the program can refer to the variable.Remember a string data type is NOT a number, even if it has all numeric digits.Assigning a variable uses the equal sign:? = . Variables can be used in place of literals.The print() function can print Integer or string values.print(type())? = returns the data type of the python objects.When using type, the result will display the data type of the information in the parenthesis: str = when python detects a string of characters (numbers, letters, punctuation…. ) in quotesint = when python detects an integer (positive or negative whole number)float = when python detects decimal numbers (i.e:? 3.1569, 0.0, 9.9999, 5.0)print() formatting of strings can be done several ways:? the + and the , are two ways. Using a comma-separating string with print() will output each object separated by a space, by default. If you are combining integer or float with strings you should use the , to avoid errors.AdditionNumeric addition: Single line math equations will output a sum for example: ? print(6 + 4)String addition (concatenation):? Single line equations will output a single concatenated string. All strings must be in quotes for example: ? print(“I wear “ + “a hat.”).ErrorsEncountering and troubleshooting errors are a fundamental part of computer programming! You will get a type error if you try to print a string with an integer. Just edit your code and change the integer to a string (use quotes).The 2 most common errors in Python are: Syntax (grammar type error – not following the rules of Python) and Name (can’t find that name).ConcatenationPython only allows like data types to be concatenated: ? str + str OR int + int. Integers and floats can be concatenated because they are both numbers.You can also add variables to the concatenation, as long as you add a string variable to strings and a number variable to numbers. input() function prompts the user to supply data returning that data as a string. The input() function ALWAYS returns the data as string data type. If a string is entered, input() returns a string. If a number is entered, input() returns a string.The input() function has an optional string argument which displays the string intended to inform a user what to enter. input() works similar to print() in the way it displays arguments as output. You can combine two lines of code into one line.BooleansData type str has methods that return a Boolean (True or False) for different tests on the properties of strings.These are some methods you can use with str:.isalpha()? = True if all characters in the string are alphabetical, else False.isalnum()? = True if all characters are alpha numeric, else False.istitle()? = True if first character of each word is capitalized, else False.isdigit()? = True if all characters are digits, else False.islower()? = True if all characters are lowercase, else False?.isupper()? = True if all characters are uppercase, else False.startwith()? = True if the string begins with the specified letter in “ “, else FalseString FormattingThe following methods can be applied to format string objects:.capitalize() = capitalizes the first character of a string, and everything after that is lowercase.lower() = all characters of a string are made lowercase.upper()= all characters of a string are made uppercase.swapcase()=all characters of a string are made to switch case (upper becomes lower and lower becomes upper).title()=each “word” separated by a space is capitalizedThe in Keyword can be used as a simple search returning True or False indication if a string is included in a target sequence. Comparing these strings is case sensitive becasue “Hello” is not the same as “hello”. ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download