How to Write and Run Your First Python Program

2.5M views
•
December 1, 2022
by
CodeWithHarry
YouTube video player
How to Write and Run Your First Python Program

TL;DR

Write print("Hello World") in a Python script, then run it to display the text. Quotation marks identify Hello World as a string, while parentheses invoke the print function. A script stores multiple instructions and executes them line by line, unlike a REPL session where commands are entered individually and evaluated as you type.

Transcript

Welcome back to 100 Days Of Code guys In today's video we'll write our Python program I'd rather say our very own first Python program Because this Python program is going to be in which we'll write every line with understanding We'll have full understanding about our Python program And we'll write this code from scratch In upcoming videos we'll wr... Read More

Key Insights

  • The print function is used to display supplied values. It is invoked by writing print followed by parentheses, with the value or object placed inside those parentheses, as demonstrated by printing "Hello World," "5," and "Bye."
  • Quotation marks identify text as a Python string. Writing print(Hello World) produces invalid syntax because Python does not recognize the unquoted words as a valid object, while print("Hello World") successfully displays the intended text.
  • A Python script stores instructions for later execution. The demonstrated main.py file contains several print statements, allowing the complete sequence to run without requiring each command to be typed manually into the terminal.
  • Python scripts execute instructions line by line. When statements printing "Hello World," "5," and "Bye" are written in that order, Python processes them in the same order and produces corresponding output.
  • The print function can accept multiple values separated by commas. Supplying "Hello World" and 7 as separate values causes both to appear in the output before the remaining statements in the script are executed.
  • A REPL follows a read, evaluate, print, loop process. After a command such as 8+9 is entered, it displays 17 and waits for another command, providing output repeatedly as new instructions are supplied.
  • Python can perform arithmetic inside the print function. The example multiplies an okra price of ₹17 per kilogram by 13 kilograms and produces a total of ₹221, showing that printed output can include computed results.
  • Arithmetic operators use specific symbols in Python. Multiplication uses an asterisk, division uses a slash, and addition and subtraction use their familiar symbols. The tutorial specifically warns against using the letter X for multiplication.

Install to Summarize YouTube Videos and Get Transcripts

Explore YouTube Video Summarizer or Get YouTube Transcript Extractor

Questions & Answers

Q: How do you write your first Python program?

Write print("Hello World") in a Python script and run it. The word print names the function, the parentheses invoke it, and the quotation marks tell Python that Hello World is a string. When the statement executes successfully, Python displays Hello World as the program's output. The tutorial builds this statement character by character to explain every part.

Q: Why does print(Hello World) cause invalid syntax?

The statement print(Hello World) causes invalid syntax because Hello World is not presented as a valid object that Python recognizes. Enclosing the words in double quotation marks changes them into a string. The corrected statement, print("Hello World"), can then be executed, and Python displays the text Hello World as expected.

Q: What does the print function do in Python?

The print function displays the value or values supplied inside its parentheses. It can print a string such as "Hello World," a value such as "5," or several values separated by commas. In the demonstration, supplying "Hello World" and 7 prints both values before Python continues with the remaining statements in the script.

Q: How does a Python script execute multiple statements?

A Python script executes its stored statements line by line. If the first statement prints "Hello World," the second prints "5," and the third prints "Bye," those outputs appear in that order. Saving the sequence in a file such as main.py means the instructions can run together instead of being entered manually one at a time.

Q: What is the difference between a Python script and a REPL?

A Python script stores a sequence of instructions and executes them line by line, making it suitable when several operations must run in a planned order. A REPL reads, evaluates, prints, and loops for each command. After evaluating an entry such as 8+9 and displaying 17, it waits for the next instruction.

Q: How can you print multiple values with one print call?

Place multiple values inside the print function's parentheses and separate them with commas. The tutorial demonstrates this by supplying "Hello World" and 7, which causes both values to appear in the output. After that print call finishes, Python continues executing the rest of the statements in the script line by line.

Q: How do you perform arithmetic in a Python print statement?

Place the arithmetic expression inside the print function's parentheses. The tutorial demonstrates multiplication using an okra price of ₹17 per kilogram and a quantity of 13 kilograms. Python evaluates the multiplication and prints ₹221 as the total. Addition, subtraction, multiplication, and division expressions can all be tried in the same way.

Q: Which symbols are used for arithmetic operations in Python?

Python uses an asterisk for multiplication and a slash for division. The tutorial warns not to use the letter X as the multiplication symbol. Addition uses the plus sign, and subtraction uses the minus sign. These operators can be placed in an expression inside print so Python calculates the result and displays it.

Summary & Key Takeaways

  • The first program uses the print function to display "Hello World." A function performs a task and is invoked with parentheses. Writing print(Hello World) causes invalid syntax because Python does not recognize the unquoted text as a valid object. Enclosing it in quotation marks identifies it as a string.

  • A Python script such as main.py stores a sequence of instructions that runs line by line. If the script prints "Hello World," then "5," and finally "Bye," the outputs appear in that same order. This avoids manually entering every instruction separately whenever the program is executed.

  • A REPL reads, evaluates, prints, and loops after each entered command. It can immediately evaluate an expression such as 8+9, but a script is preferable when Python must process a planned sequence of instructions. Python can also calculate values using operators such as multiplication, division, addition, and subtraction.


Read in Other Languages (beta)

Share This Summary 📚

Explore More Summaries from CodeWithHarry 📚