The document covers the basics of Python programming, focusing on syntax differences between Python and Java, input/output functions, and variable assignment. It includes examples of using the print() and input() functions, as well as exercises for the reader to practice these concepts. Additionally, it outlines variable naming rules and the importance of precision in programming.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
3 views6 pages
Python Turorail
The document covers the basics of Python programming, focusing on syntax differences between Python and Java, input/output functions, and variable assignment. It includes examples of using the print() and input() functions, as well as exercises for the reader to practice these concepts. Additionally, it outlines variable naming rules and the importance of precision in programming.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6
CONCEPTS IN PRACTICE
Python vs Java syntax
5. In general, Python programs are _____ than Java programs. a. faster b. longer c. shorter 6. In the example programs above, what syntax required by Java is not required by Python? a. semicolons b. parentheses c. quote marks TRY IT Favorite song The program below asks for your name and displays a friendly greeting. Run the program and see what happens. In the error message, EOF stands for End of File. • Many of the programs in this chapter expect input from the user. Enter your name in the Input box below the code. Run the program again, and see what changes. • Copy the following lines to the end of the program: print("What is your favorite song?") song = input() print("Cool! I like", song, "too.") • The modified program reads two lines of input: name and song. Add your favorite song to the Input box below your name, and run the program again. The next section of the book will explain how print() and input() work. Feel free to experiment with this code until you are ready to move on. Access multimedia content (https://openstax.org/books/introduction-python-programming/pages/ 1-1-background) 1.2 Input/output Learning objectives By the end of this section you should be able to • Display output using the print() function. • Obtain user input using the input() function. Basic output The print() function displays output to the user. Output is the information or result produced by a program. The sep and end options can be used to customize the output. Table 1.1 shows examples of sep and end. Multiple values, separated by commas, can be printed in the same statement. By default, each value is separated by a space character in the output. The sep option can be used to change this behavior. By default, the print() function adds a newline character at the end of the output. A newline character tells 10 1 • Statements Access for free at openstax.org the display to move to the next line. The end option can be used to continue printing on the same line. Code Output print("Today is Monday.") print("I like string beans.") Today is Monday. I like string beans. print("Today", "is", "Monday") print("Today", "is", "Monday", sep="...") Today is Monday Today...is...Monday print("Today is Monday, ", end="") print("I like string beans.") Today is Monday, I like string beans. print("Today", "is", "Monday", sep="? ", end="!!") print("I like string beans.") Today? is? Monday!!I like string beans. Table 1.1 Example uses of print(). CHECKPOINT Displaying output to the user Access multimedia content (https://openstax.org/books/introduction-python-programming/pages/ 1-2-inputoutput) CONCEPTS IN PRACTICE The print() function 1. Which line of code prints Hello world! as one line of output? a. print(Hello world!) b. print("Hello", "world", "!") c. print("Hello world!") 2. Which lines of code prints Hello world! as one line of output? a. print("Hello") print(" world!") b. print("Hello") print(" world!", end="") c. print("Hello", end="") print(" world!") 3. What output is produced by the following statement? print("555", "0123", sep="-") 1.2 • Input/output 11 a. 555 0123 b. 5550123- c. 555-0123 DO SPACES REALLY MATTER? Spaces and newline characters are not inherently important. However, learning to be precise is an essential skill for programming. Noticing little details, like how words are separated and how lines end, helps new programmers become better. Basic input Computer programs often receive input from the user. Input is what a user enters into a program. An input statement, variable = input("prompt"), has three parts: 1. A variable refers to a value stored in memory. In the statement above, variable can be replaced with any name the programmer chooses. 2. The input() function reads one line of input from the user. A function is a named, reusable block of code that performs a task when called. The input is stored in the computer's memory and can be accessed later using the variable. 3. A prompt is a short message that indicates the program is waiting for input. In the statement above,"prompt" can be omitted or replaced with any message. CHECKPOINT Obtaining input from the user Access multimedia content (https://openstax.org/books/introduction-python-programming/pages/ 1-2-inputoutput) CONCEPTS IN PRACTICE The input() function 4. Which line of code correctly obtains and stores user input? a. input() b. today_is = input c. today_is = input() 5. Someone named Sophia enters their name when prompted with print("Please enter your name: ") name = input() What is displayed by print("You entered:", name)? a. You entered: name b. You entered: Sophia 12 1 • Statements Access for free at openstax.org c. You entered:, Sophia 6. What is the output if the user enters "six" as the input? print("Please enter a number: ") number = input() print("Value =", number) a. Value = six b. Value = 6 c. Value = number TRY IT Frost poem Write a program that uses multiple print() statements to output the following poem by Robert Frost (https://openstax.org/r/100robertfrost). Each print() statement should correspond to one line of output.Tip: You don't need to write the entire program all at once. Try writing the first print() statement, and then click the Run button. Then write the next print() statement, and click the Run button again. Continue writing and testing the code incrementally until you finish the program. I shall be telling this with a sigh Somewhere ages and ages hence: Two roads diverged in a wood, and I-- I took the one less traveled by, And that has made all the difference. Access multimedia content (https://openstax.org/books/introduction-python-programming/pages/ 1-2-inputoutput) TRY IT Name and likes Write a program that asks the following two questions (example input in bold): Shakira What do you like? singing What is your name? Shakira What do you like? singing Output a blank line after reading the input. Then output the following message based on the input: 1.2 • Input/output 13 Shakira likes singing Shakira likes singing Access multimedia content (https://openstax.org/books/introduction-python-programming/pages/ 1-2-inputoutput) 1.3 Variables Learning objectives By the end of this section you should be able to • Assign variables and print variables. • Explain rules for naming variables. Assignment statement Variables allow programs to refer to values using names rather than memory locations. Ex: age refers to a person's age, and birth refers to a person's date of birth. A statement can set a variable to a value using the assignment operator (=). Note that this is different from the equal sign of mathematics. Ex: age = 6 or birth = "May 15". The left side of the assignment statement is a variable, and the right side is the value the variable is assigned. CHECKPOINT Assigning and using variables Access multimedia content (https://openstax.org/books/introduction-python-programming/pages/ 1-3-variables) CONCEPTS IN PRACTICE Assigning and using variables 1. Which line of code correctly retrieves the value of the variable, city, after the following assignment? city = "Chicago" a. print("In which city do you live?") b. city = "London" c. print("The city where you live is", city) 2. Which program stores and retrieves a variable correctly? a. print("Total =", total) total = 6 b. total = 6 print("Total =", total) c. print("Total =", total) 14 1 • Statements Access for free at openstax.org total = input() 3. Which is the assignment operator? a. : b. == c. = 4. Which is a valid assignment? a. temperature = 98.5 b. 98.5 = temperature c. temperature - 23.2 Variable naming rules A variable name can consist of letters, digits, and underscores and be of any length. The name cannot start with a digit. Ex: 101class is invalid. Also, letter case matters. Ex: Total is different from total. Python's style guide recommends writing variable names in snake case, which is all lowercase with underscores in between each word, such as first_name or total_price. A name should be short and descriptive, so words are preferred over single characters in programs for readability. Ex: A variable named count indicates the variable's purpose better than a variable named c. Python has reserved words, known as keywords, which have special functions and cannot be used as names for variables (or other objects).