🎧 New: AI-Generated Podcasts Turn your study notes into engaging audio conversations. Learn more

Copy of Chapter 1 - Statements.pdf

Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...

Full Transcript

OBJECT-ORIENTED PROGRAMMING USING PYTHON Chapter 1: Statements What are computers? What are programs? Introduction to Python Libraries 01 Standard Library Comes with Python, offering tools for common tasks like data extraction and report ge...

OBJECT-ORIENTED PROGRAMMING USING PYTHON Chapter 1: Statements What are computers? What are programs? Introduction to Python Libraries 01 Standard Library Comes with Python, offering tools for common tasks like data extraction and report generation. External Library 02 Developed by the community for various purposes, such as Pandas for data analysis. Python 1. Conciseness 2. structure Syntax Python Java public class Hello { public static void main(String[] args) { print("Hello, World!") System.out.println("Hello, World!"); } } Try: print("What is your favorite song?") song = input() print("Cool! I like", song, "too.") Input/ Output Basic print() displays output. Use sep and end to Output customize output. Code Output print("Today is Monday.") Today is Monday. print("I like string beans.") I like string beans. print("Today", "is", "Monday") Today is Monday print("Today", "is", "Monday", sep="...") Today...is...Monday print("Today is Monday, ", end="") Today is Monday, I like stringbeans. print("I like string beans.") print("Today", "is", "Monday", sep="? ",end="!!") Today? is? Monday!!I like print("I like string beans.") stringbeans. Basic variable = input("prompt") Input Try: # Ask the user for their name name = input("What is your name? ") # Ask the user what they like likes = input("What do you like? ") # Show the user's name and what they like print("Your name is " + name + ".") print("You like " + likes + ".") Variables 01 Assignment Statement Sets a variable to a value using the assignment operator (`=`) Variables 02 Variable Name Rules Can include letters, digits, and underscores; cannot start with a digit. Letter case matters. 03 Name Descriptions Should be short and descriptive. Use words instead of single characters for Variables better readability 04 Reserved Words Python has reserved keywords with special functions that cannot be used as variable names. String Basics A string is a sequence of String characters enclosed by Basics matching single (`'`) or double (`"`) quotes The len() len() function, when called on a string function value, returns the string length. number = "12" number_of_digits = len(number) print("Number", number, "has", number_of_digits, "digits.") Concatenation combines two or more strings sequentially concatenation using the + operator. Example: "A" + "part" produces the string "Apart". Number Basics Numeric Integer Floating-point Data Types Used for String representing text x=1 y = 2.0 s = "32" print(x, type(x)) 1 print(y, type(y)) 2.0 print(s, type(s)) 32 Used to perform Basic mathematical operations like addition, subtraction, Arithmetic multiplication, and division. Operator Operators are evaluated in order of precedence. Precedence Operator Description Example Result () Parentheses (1 + 2) * 3 9 ** Exponentiation 2 ** 4 16 +,- Positive, negative -math.pi -3.14159 Multiplication, *,/ 2*3 6 division Addition, +,- 1+2 3 subtraction Single-line Comments: Begin with the # symbol. Comments Multi-line Comments: Use triple quotes (''' or """) What is the name of your dish? Pancakes How many servings does the current ACTIVITY recipe make? 8 How many cups of flour are required for Create a Python script that interacts with the current recipe? 2.0 the user to collect information for a meal How many cups of sugar are required for plan. The script should prompt the user the current recipe? 1.0 for the dish name, the number of servings How many cups of butter are required for the recipe currently makes, and the the current recipe? 0.5 quantities of three ingredients (such as How many servings do you want to make? flour, sugar, and butter). It should then 12 prompt for the desired number of servings and calculate the amounts of Dish Name: Pancakes each ingredient needed for the new Original Servings: 8 serving size. The script should handle To make 12 servings, you need: - 3.00 cups of flour potential input errors gracefully and - 1.50 cups of sugar include comments to explain each part of - 0.75 cups of butter the code.

Use Quizgecko on...
Browser
Browser