Python Basics for Computer Science 105 Dr Neil
Python Basics for Computer Science 105 Dr. Neil Simonetti for a more complete treatment of the basics see https: //python. swaroopch. com/basics. html
Programming Tips • Anything typed on a line after a pound sign [#] is ignored. • Use this to write comments. It is helpful to other programmers and your future self. • One statement per line is recommended. • If you really want to put two statements on a line, separate them with a semicolon [ ; ] • If you want to continue a statement on another line, end the first with a backslash [ ] • Indenting has meaning. Only use indenting when it is required. • You will learn later when items need to be indented. • When writing larger programs, test frequently. • Try to test that what you have written is correct at multiple stages, so that when an error happens, you have a good idea where to look.
Identifiers Literals are names given to a portion of computer memory, used to store information. are pieces of information stored on a computer. Think of literals as things that go Think of identifiers into boxes. as names of boxes.
Identifiers Literals • Are case sensitive. • May contain letters, numbers, or underscores, but may not start with a number. • Are never in quotes. • May not duplicate a “reserved word” (words used as Python commands or statements) such as print, input, if, or while. • Are also case sensitive. • Come in two different types, strings and numbers. • Strings are always in quotes, but the quotes are not part of the string. There is no difference between single and double quotes. • Strings may contain any character. • Numbers are never in quotes. • Numbers have two subtypes: integer (whole) and float (decimal).
Identifiers Examples: • x • a 3 • counter • My_File • case. List • my_answer_42 • a_really_long_name • another. Long. Name 47 q Literals Examples: • 3 (integer number) • – 46 (integer number) • 12. 0 (float number) • 3. 14159 (float number) • "hello, world!" (string) • "467" (string) • "@#$%!!" (string) • "" (the empty string)
Assignment Statements • Values (literals or expressions) can be put into identifiers (boxes) with an assignment statement using the assignment operator [=] • Put the identifier to the left, and the value to go inside on the right. • Only identifiers can be put on the left side of an assignment operator. • Identifiers, literals, or expressions involving a legal combination of these may be on the right side. • x = 12 • a 3 = "hello, world!" • my_answer = 4 + x / 3
More Assignment Examples Strings have a red background. Integers have green. Floats have blue. x = 12 a 3 = "llamas rule!" count = 4 my_answer = x your_answer = "x" my_answer = x / count = count + 1 x = your_answer + "yz" 12 x xyz a 3 llamas rule! 4 count 5 12 my_answer 3. 0 your_answer x #division result is float #updates value of count #strings may be added
Invalid Assignment Statements • "x" = 12 • You may not assign anything into a string literal. • 37 = x • You may not assign anything into a numeric literal. • x = "37" + 37 • Strings and numbers may not be added together. • print = 3. 14159265 • You may not use reserved words as identifiers.
Combining Types with Common Operators unless (parentheses) are used, all multiplications and divisions are done before any additions and subtractions Addition Subtraction + string integer float - string integer float string error error integer float integer error integer float error float Multiplication Division * string integer float / string integer float string error error integer string integer float integer error float* float error float 3 * "ha" results in "hahaha". 0 * "ha" results in "" *In Python 2, integer / integer is a truncated integer.
Converting Types • Any integer or float can be converted to a string using the str() function. • The string contains the most “normal” way to represent the number. • Integers may be converted to floats with the float() function. • Floats can be recognized by having at least one number after a decimal point. • Floats may be converted to integers with the int() function. • Numbers are always rounded down (toward zero). • Strings may also be converted to floats or integers with the float() or int() function. • If the string is not in a recognized numeric format, this creates an error.
Conversion examples • str(5+3) • str(5. 300) • str(5)+str(3) • float(5*3) • int(7. 8) • int(-5/3) • float("5") • int("5") • float("five") • int("5. 9") • int(float("5. 9")) returns "8" returns "5. 3" returns "53" returns 15. 0 returns 7 returns – 1 returns 5. 0 returns 5 produces an error returns 5
print() • The print() function will print strings without quotes. • It will print floats with at least one number after a decimal point. • It will add a space between items separated by a comma. • You can get around this by combining strings with addition or using format(). • The line: print("Six divided by three is", 6/3) will print: Six divided by three is 2. 0 • It will add a “new line” character (carriage return) to the end of the printed material. You can suppress this by specifying an end. • print("Six divided by three is", 6/3, end="")
input() • The input() command will always return a string. • If you want the user to enter a number, you must convert with float() or int(). • Put the prompting text in parentheses. • x = input("How old are you? ") assigns x to a string. • x = int(input("How old are you? ")) assigns x to an integer, but it will generate an error if the user enters non-numeric text.
- Slides: 13