From “Learn Python in Less than 10 Minutes for Beginners (Fast & Easy)” by Indently
Python basics in under ten minutes: variables to try-except
Python in under ten minutes, typed live in PyCharm.
Key points
- Python converts types only when you ask it to: wrap an integer in str() to join it with text, and wrap a digit string in int() before adding a number to it, otherwise the program raises an exception. 2:35
- Python is case sensitive and counts from zero, so a variable named i and one named I are different, and range(3) produces 0, 1 and 2 unless you add one yourself. 0:40
- A while True loop never ends on its own, so it needs a break statement, as in the example that stops when the user enters '0'. 6:20
- Functions are defined with the def keyword and exist so one line of code can be reused, and pass lets you leave a function empty as a placeholder while the program still runs. 7:16
- Wrapping risky code in try and except keeps the program running when input is bad: entering 'mario' where a number is expected shows a friendly message instead of crashing. 9:14
Terms
- String — Any form of text the program reads, written inside quotation marks. 0:33
- Case sensitive — item and Item are treated as different names. 0:40
- Boolean — A simple true or false value. 1:47
- List — A collection of values written inside square brackets. 1:58
- Function — A named block of code defined with def that can be reused many times. 7:33
- pass — A keyword that holds the place of an empty function body. 8:30
- Exception — An error that occurs while running code; try and except handle it. 9:31
What you need
- Python 3.8 — Used as the interpreter in the video.
- PyCharm — Code editor used for all examples.
Steps
- 1. Assign a variable: Choose a name such as item and assign a value with =. 0:21
- 2. Write an if statement: Use if with a condition, then elif and else for other cases. 4:25
- 3. Write a for loop: Use for i in range(3) and print the loop value. 5:27
- 4. Wrap risky code in try: Put the conversion in try and print a message in except. 9:26
Look closer on screen
- TypeError traceback — Run panel shows TypeError: can only concatenate str (not "int") to str.
- Input loop prompt — Run console shows Enter something >> prompts with 10 and 02 entered.
Arithmetic results on a=10, b=5
- : addition a + b · : 15
- : difference a - b · : 5
- : multiply a * b · : 50
- : divide a/b · : 2.0
- : power a**b · : 100000