YouTube2Text

Python for Beginners - Learn Coding with Python in 1 Hour — Transcript

by Programming with Mosh · 9,840 words · 1,642 segments · language en · Watch on YouTube

Full transcript

  1. 0:02In this Python tutorial, you're going to
  2. 0:03learn everything you need to know to
  3. 0:05start programming in Python. If you want
  4. 0:07to learn Python programming for data
  5. 0:08science, machine learning, or web
  6. 0:10development, this Python tutorial is the
  7. 0:12perfect place to learn Python. You don't
  8. 0:14need any prior knowledge in Python or
  9. 0:16programming in general. I'm going to
  10. 0:18teach you everything from scratch. I'm
  11. 0:20Mosh Hamedani, and I've taught millions
  12. 0:22of people how to code through this
  13. 0:23channel. If you're new here, make sure
  14. 0:25to subscribe as I upload new videos
  15. 0:26every week. Now, let's jump in and get
  16. 0:28started.
  17. 0:30All right, before we get started, let me
  18. 0:31give you some ideas about what you can
  19. 0:32do with Python. That's a very common
  20. 0:34question. Python is a multi-purpose
  21. 0:36programming language, so you can use it
  22. 0:38for a variety of different tasks. You
  23. 0:40can use Python for machine learning and
  24. 0:42AI. In fact, Python is the number one
  25. 0:44language for machine learning and data
  26. 0:45science projects. Python is also very
  27. 0:47popular in web development. Using Python
  28. 0:50and a framework called Django, you can
  29. 0:52build amazing websites. Here are five
  30. 0:54websites powered with Python and Django:
  31. 0:56YouTube, Instagram, Spotify, Dropbox,
  32. 1:00and Pinterest. You can also use Python
  33. 1:02in automation. With Python, you can save
  34. 1:04your time and increase your productivity
  35. 1:06by automating repetitive tasks. So, why
  36. 1:09are you learning Python? Are you
  37. 1:10learning it for automation, for data
  38. 1:12science, or web development? Let me know
  39. 1:14in the comments section below.
  40. 1:19All right, the first thing I want you to
  41. 1:20do is to head over to python.org to
  42. 1:23download the latest version of Python.
  43. 1:26So, you go to downloads and select the
  44. 1:28latest version of Python.
  45. 1:33Here in your downloads folder, you
  46. 1:34should see this package. Simply
  47. 1:36double-click it.
  48. 1:38You're going to see this Python
  49. 1:38installer.
  50. 1:40If you're on Windows, you will see this
  51. 1:41checkbox over here, add Python to path.
  52. 1:45Make sure to check it. It's really
  53. 1:46important. Otherwise, you're not going
  54. 1:48to be able to follow this tutorial.
  55. 1:51Simply click on continue
  56. 1:52again
  57. 1:53one more time
  58. 1:55I agree with the terms
  59. 1:57and install the latest version of
  60. 1:59Python.
  61. 2:00Now, here you need to enter the username
  62. 2:02and password of your computer. So,
  63. 2:05let's do that real quick.
  64. 2:09Next, you need to install a code editor.
  65. 2:11We use a code editor to write our code
  66. 2:13and execute it. The most popular code
  67. 2:15editor for Python is PyCharm. You can
  68. 2:17get it from jetbrains.com/pycharm.
  69. 2:21So, on this page
  70. 2:23click on download.
  71. 2:26You should see two different editions.
  72. 2:27One is the professional edition, which
  73. 2:29is commercial. And we also have this
  74. 2:31community edition, which is free and
  75. 2:33open source. So, we're going to download
  76. 2:35the community edition.
  77. 2:39Now, in your downloads folder, you
  78. 2:41should have this package. Let's
  79. 2:42double-click it.
  80. 2:45If you're on Windows, you're going to
  81. 2:46see an installation wizard. So, simply
  82. 2:48click on the next button until you
  83. 2:50install PyCharm. If you're on a Mac, you
  84. 2:53need to drag this PyCharm and drop it
  85. 2:55onto the applications folder.
  86. 3:00Now
  87. 3:01let's open it.
  88. 3:03The first time you open PyCharm, you
  89. 3:04have to configure a few settings. We
  90. 3:06don't want to spend time on this. So,
  91. 3:08over here, we're going to click on skip
  92. 3:10remaining and set defaults.
  93. 3:14Now, let's create a new project.
  94. 3:16Over here, we can specify the location
  95. 3:18and the name of our Python project. So,
  96. 3:22let's append hello world to this path.
  97. 3:25This is where our Python project is
  98. 3:26going to be saved. So, let's click on
  99. 3:29create.
  100. 3:32In this window, you can see the content
  101. 3:34of our project. So, here's our hello
  102. 3:36world project. Currently, we have only
  103. 3:38one folder inside this project, that is
  104. 3:40VN, which is short for virtual
  105. 3:42environment. We'll talk about virtual
  106. 3:43environments in the future. So,
  107. 3:45currently, we don't have any Python
  108. 3:47files inside this project. A real
  109. 3:49application can consist of tens or
  110. 3:51hundreds or even thousands of Python
  111. 3:53files. So, let's right-click on the
  112. 3:55project name
  113. 3:57and go to new Python file.
  114. 4:00We're going to call this file app.
  115. 4:04Now, we can collapse this project window
  116. 4:05by clicking on this icon.
  117. 4:07So, now we have more space. Let's write
  118. 4:09our first Python code.
  119. 4:11We're going to write print, all in
  120. 4:12lowercase
  121. 4:14then add parentheses
  122. 4:15then add quotes, either single quotes or
  123. 4:18double quotes.
  124. 4:20And inside these quotes, we're going to
  125. 4:21write hello world.
  126. 4:23So, this is what we call a string. A
  127. 4:25string means a string or sequence of
  128. 4:28characters. In simple words, that means
  129. 4:30textual data. So, in Python and in many
  130. 4:33other programming languages, whenever
  131. 4:35we're dealing with textual data, we
  132. 4:37should always surround our text with
  133. 4:39quotes.
  134. 4:40In Python, we can use single or double
  135. 4:42quotes.
  136. 4:44Now, this print you see here is a
  137. 4:45function built into Python, and we can
  138. 4:48use it to print a message on our
  139. 4:49application window.
  140. 4:51So, let me show you how to run this
  141. 4:52code.
  142. 4:54On the top we go to the run menu and
  143. 4:56then select run. Note that there is a
  144. 4:59shortcut associated with this command. I
  145. 5:01always use shortcuts because they
  146. 5:02increase my productivity. So,
  147. 5:05let's click on this. Now, select app
  148. 5:09and over here
  149. 5:10you can see this little window. This is
  150. 5:12what we call the terminal window, and it
  151. 5:14shows the output of our program.
  152. 5:16So, here's the hello world message
  153. 5:18printed in the terminal window. Now, as
  154. 5:20you learn more Python, you will learn
  155. 5:22how to build applications that have a
  156. 5:23graphical user interface. That's an
  157. 5:25advanced topic, so for now, let's not
  158. 5:27worry about it.
  159. 5:34All right, now let's talk about
  160. 5:35variables. We use variables to
  161. 5:37temporarily store data in a computer's
  162. 5:40memory. For example, we can store the
  163. 5:42price of a product or someone's name,
  164. 5:44their email, their age, and so on. Let
  165. 5:47me show you. So,
  166. 5:48to declare a variable, we start by
  167. 5:50typing a name for that variable. Let's
  168. 5:52say age.
  169. 5:54Then we add an equal sign
  170. 5:56and then we type a value. Let's say 20.
  171. 5:59So, with this, we're storing the number
  172. 6:0020 somewhere in our computer's memory
  173. 6:03and we're attaching this age as a label
  174. 6:05for that memory location. So, now we can
  175. 6:08read the value at this memory location
  176. 6:10and print it on the terminal. So,
  177. 6:12instead of printing hello world, we want
  178. 6:14to print the value of the age variable.
  179. 6:17So, I'm going to delete what we have
  180. 6:19inside parentheses
  181. 6:21and type age. Note that I'm not adding
  182. 6:25quotes because if I run this program,
  183. 6:27we'll see the text age on the terminal.
  184. 6:29We don't want that. We want the value of
  185. 6:31the age variable. So,
  186. 6:34let's remove the quotes
  187. 6:35and print the value of the age variable.
  188. 6:38Now, here on the toolbar, you can click
  189. 6:41on this play icon to run your program,
  190. 6:43or you can use the shortcut that I
  191. 6:44showed you in the last video. So, the
  192. 6:47shortcut is over here. On a Mac, that's
  193. 6:49control, shift, and R.
  194. 6:52So,
  195. 6:53there you go.
  196. 6:55Now, you can see the value of the age
  197. 6:56variable.
  198. 6:58Now, we can also change the value of a
  199. 6:59variable. For example, on line two
  200. 7:02we can set age to 30.
  201. 7:05Now, when we run our program
  202. 7:07we see 30. So, as you can see, our
  203. 7:09program gets executed from top to
  204. 7:11bottom.
  205. 7:13So, this is how we can declare and use a
  206. 7:14variable. Now, let's look at a few more
  207. 7:16examples.
  208. 7:17So, I'm going to declare another
  209. 7:19variable called price and set it to
  210. 7:2219.95.
  211. 7:25So, in Python, we can use numbers with a
  212. 7:26decimal point or whole numbers.
  213. 7:30We can also declare a variable and
  214. 7:32assign it a string value. So, let's say
  215. 7:35first_name.
  216. 7:38So, if you want to use multiple words in
  217. 7:40the name of a variable, we should
  218. 7:41separate them using an underscore. This
  219. 7:44makes our code more readable. See what
  220. 7:46would happen if I didn't use this
  221. 7:48underline.
  222. 7:50This is not easily readable. So, we
  223. 7:52always separate multiple words by an
  224. 7:54underscore.
  225. 7:56Now, we set this to a string. So, we can
  226. 7:58use single quotes or
  227. 8:00double quotes.
  228. 8:01Let's say Mosh.
  229. 8:02We also have a special type of value
  230. 8:04called a boolean value, which can be
  231. 8:06true or false. That is like yes or no in
  232. 8:09English. Let me show you. So,
  233. 8:12I'm going to declare another variable
  234. 8:14called is_online
  235. 8:16and set it to true.
  236. 8:18We could also set it to false. What we
  237. 8:21have here is called a boolean value.
  238. 8:23Now, note that Python is a
  239. 8:25case-sensitive language, so it's
  240. 8:27sensitive to lowercase and uppercase
  241. 8:29letters. In this case, if I use a
  242. 8:31lowercase f
  243. 8:34we can see an error over here because
  244. 8:35this is not recognized in Python.
  245. 8:38So, false with a capital F is a special
  246. 8:40keyword in Python that represents the
  247. 8:43boolean false value.
  248. 8:45So, this is how we can declare and use
  249. 8:47variables in Python.
  250. 8:48All right, now here's a little exercise
  251. 8:50for you. Imagine you want to write a
  252. 8:52program for a hospital. So, we're going
  253. 8:54to check in a patient named John Smith.
  254. 8:56He's 20 years old and is a new patient.
  255. 8:59I want you to declare a few variables to
  256. 9:01store these values. Use the comment box
  257. 9:03below to share your code with others.
  258. 9:12In this tutorial, I'm going to show you
  259. 9:13how to receive input from the user.
  260. 9:16So, in Python, we have another built-in
  261. 9:18function called input. We use this to
  262. 9:20read a value from the terminal window.
  263. 9:22Let me show you. So,
  264. 9:24we add parentheses
  265. 9:26then we type a string. Here, we can type
  266. 9:28a message like what is your name? We add
  267. 9:31a question mark followed by a space.
  268. 9:34You'll see why we need this space in a
  269. 9:35second. So, let's run this program.
  270. 9:39We get this message. Now, we have to
  271. 9:41enter a value. So, we click over here.
  272. 9:44Now, you can see that the carrot is
  273. 9:45separated from the question mark. This
  274. 9:47is because of the white space that we
  275. 9:49added over here.
  276. 9:51So, now we have to type a value. Let's
  277. 9:53say John. When we press enter
  278. 9:56this function will return the value that
  279. 9:58we entered in the terminal window. So,
  280. 10:01we can get that value and store it in a
  281. 10:02variable.
  282. 10:04So, let's declare a variable called name
  283. 10:06and set it to the return value of the
  284. 10:09input function.
  285. 10:10Now, we can print a greeting message for
  286. 10:12this user. So,
  287. 10:14we use the print function.
  288. 10:16We say, "Hello."
  289. 10:18We add a space.
  290. 10:20Now, after this string, we want to add
  291. 10:22the value of the name variable. So, we
  292. 10:24use the plus sign and then type name.
  293. 10:27What we're doing here is called string
  294. 10:30concatenation. So, we're combining this
  295. 10:32string with another string.
  296. 10:35Now, let's run our program and see what
  297. 10:36happens. So, what is your name? Mosh.
  298. 10:40Now,
  299. 10:41we get this message, "Hello Mosh."
  300. 10:43So, this is how we can use the input
  301. 10:44function in Python.
  302. 10:52You'll learn about the three types of
  303. 10:53data in Python. We have numbers,
  304. 10:56strings, and booleans.
  305. 10:59Now, there are times you want to convert
  306. 11:00the value of a variable from one type to
  307. 11:02another. Let me show you. So, we're
  308. 11:05going to use our input function
  309. 11:07to read the user's birth year. So,
  310. 11:10"Enter your birth year."
  311. 11:13Now, this input function is going to
  312. 11:15return a value, so we can store it in a
  313. 11:17variable called
  314. 11:19birth_year.
  315. 11:21Okay?
  316. 11:22Now, let's write code to calculate the
  317. 11:24age of this user. So, we write an
  318. 11:26expression like this. Currently, we are
  319. 11:28in the year 2020. So, 2020 minus
  320. 11:32birth year.
  321. 11:34This expression or piece of code is
  322. 11:35going to produce a value. So, once
  323. 11:37again, we can store that value in a
  324. 11:39variable. Let's call that variable age.
  325. 11:43Now, let's print age
  326. 11:45on the terminal.
  327. 11:46Let's run our program and see what
  328. 11:47happens.
  329. 11:49So, my birth year is 1982.
  330. 11:52Enter. Oops, our program crashed. So,
  331. 11:55whenever you see this red message, that
  332. 11:57indicates an error.
  333. 11:59So, this error occurred in this file,
  334. 12:02that is our app.py, on line two.
  335. 12:05Right below that, you can see the piece
  336. 12:07of code that generated this error.
  337. 12:09So, that is this expression, 2020 minus
  338. 12:12birth year. Now, below that, you can see
  339. 12:14the type of error. So, here we have
  340. 12:17unsupported types for subtraction.
  341. 12:21We have int and str.
  342. 12:24What are these? Well, this int is short
  343. 12:26for integer, and that represents a whole
  344. 12:28number in programming. So,
  345. 12:312020 is an example of an integer.
  346. 12:34Now, birth year
  347. 12:36is an example of a string because
  348. 12:38whenever we call the input function,
  349. 12:40this function will return a value as a
  350. 12:42string, even if we enter a number. In
  351. 12:45other words, when I entered 1982, this
  352. 12:48input function returned a string with
  353. 12:50these characters, 1982. So, this string
  354. 12:53is different from the number 1982.
  355. 12:57They're completely different types. So,
  356. 12:59in this case,
  357. 13:00let me delete these lines.
  358. 13:02The reason we got this error is that we
  359. 13:04tried to subtract a string from an
  360. 13:07integer. So, our code looked like this,
  361. 13:101982.
  362. 13:12Now, Python doesn't know how to subtract
  363. 13:14a string from an integer.
  364. 13:16So, to solve this problem,
  365. 13:18we need to convert this string to an
  366. 13:19integer.
  367. 13:21Now, in Python, we have a bunch of
  368. 13:22built-in functions for converting the
  369. 13:24types of our variables. So, we have this
  370. 13:27int function.
  371. 13:29We can pass our birth year to it,
  372. 13:31and this will return the numeric
  373. 13:33representation of the birth year. So, to
  374. 13:36solve this problem,
  375. 13:38we need to replace
  376. 13:40the string
  377. 13:42with the int function.
  378. 13:43So, let's see what's going on here.
  379. 13:45On the first line, we call the input
  380. 13:47function. This returns a string.
  381. 13:49On the second line, we pass the string
  382. 13:52to our int function. The int function
  383. 13:54will return the numeric representation
  384. 13:56of the birth year.
  385. 13:58Then,
  386. 13:59we subtract it from 2020, we get the
  387. 14:01age, and store it in the age variable.
  388. 14:04Now, let's run our program.
  389. 14:06So, 1982,
  390. 14:08and there you go. I'm 38 years old.
  391. 14:11So, this is how the int function works.
  392. 14:14Now, we also have another built-in
  393. 14:16function called float. That is for
  394. 14:18converting a value to a floating-point
  395. 14:20number.
  396. 14:21A floating-point number in Python and
  397. 14:23other programming languages is a number
  398. 14:25with a decimal point. So, 10 is an
  399. 14:29integer, and 10.1 is a float.
  400. 14:32So, we have int, we have float, and we
  401. 14:35also have bool
  402. 14:36for converting a value to a boolean.
  403. 14:39And finally, we have str
  404. 14:41for converting a value to a string.
  405. 14:44So, these are the built-in functions for
  406. 14:46converting the type of our variables.
  407. 14:48Now, here's a little exercise for you. I
  408. 14:50want you to write a basic calculator
  409. 14:52program.
  410. 14:53So, here we have to enter two numbers.
  411. 14:55You can type a whole number or a number
  412. 14:57with a decimal point.
  413. 15:00And then our program will print the sum
  414. 15:02of these two numbers.
  415. 15:03So, pause the video, spend two minutes
  416. 15:05on this exercise, and then see my
  417. 15:07solution.
  418. 15:11All right, first we're going to call our
  419. 15:12input function to read the first number.
  420. 15:15We get the result and store it in a
  421. 15:16variable called first.
  422. 15:19Now, let's declare
  423. 15:21another variable called second and read
  424. 15:24the second number.
  425. 15:26Now,
  426. 15:27we calculate the sum.
  427. 15:29So, that is first plus second.
  428. 15:32Now, let's see what happens when we
  429. 15:33print sum on the terminal.
  430. 15:36So, I enter 10 and 20,
  431. 15:39but instead of 30, we get 1020. This is
  432. 15:42because we're combining or concatenating
  433. 15:44two strings. So,
  434. 15:47as I told you before, the input function
  435. 15:49returns a string. So,
  436. 15:52this line will be equivalent to first
  437. 15:55equals 10.
  438. 15:56We're dealing with a string, not an
  439. 15:58integer.
  440. 15:59Similarly,
  441. 16:00second is going to be
  442. 16:0220 as a string. So, when we combine two
  443. 16:05strings, 10 plus 20, we'll get 1020
  444. 16:10because we're dealing with textual data,
  445. 16:12okay? So, to solve this problem,
  446. 16:16we need to convert the values we read to
  447. 16:18their numeric representation. So, over
  448. 16:20here,
  449. 16:23we're going to pass first to our int
  450. 16:25function,
  451. 16:27and here as well.
  452. 16:28Now, let's run our program.
  453. 16:30So, we enter 10 and 20, we get 30.
  454. 16:34What if we enter a floating-point
  455. 16:35number? So, 10.1 and 20.
  456. 16:40We got an error.
  457. 16:41So, to solve this problem,
  458. 16:44we need to treat both these values as
  459. 16:46floats. So, instead of the int function,
  460. 16:49we're going to use the float function.
  461. 16:52Now, let's run our program one more
  462. 16:54time.
  463. 16:55We enter a whole number and a
  464. 16:57floating-point number.
  465. 16:59So, the result is correct.
  466. 17:01Now, let's add a label over here. So,
  467. 17:04sum
  468. 17:05is
  469. 17:07plus sum.
  470. 17:08Let's run our program
  471. 17:10one more time. 10 and 20.
  472. 17:12Once again, we got an error. The error
  473. 17:14is saying that Python can only
  474. 17:16concatenate strings, not floats to
  475. 17:18strings.
  476. 17:21So, on line four, we have a string.
  477. 17:23We're concatenating this with a float
  478. 17:26because the result of this expression is
  479. 17:28a floating-point number. We're adding
  480. 17:30two floats, so the result is a float as
  481. 17:32well. So, Python doesn't know how to
  482. 17:35evaluate code like this.
  483. 17:38It doesn't know how to concatenate a
  484. 17:40float
  485. 17:42to a string. To solve this problem,
  486. 17:45we need to convert sum to a string. So,
  487. 17:47this is where
  488. 17:48we use the str function.
  489. 17:51Now, let's run the program again. So, 10
  490. 17:54plus 20.1, and here's the result.
  491. 17:58And one last thing.
  492. 17:59In this example, I'm calling the float
  493. 18:01function at the time we want to
  494. 18:03calculate the sum of these two numbers,
  495. 18:05but this is not a requirement. You can
  496. 18:07call the float function
  497. 18:09over here. So, this input function
  498. 18:11returns a string. We can pass that
  499. 18:14string to our float function. Take a
  500. 18:16look. So, float parentheses like this.
  501. 18:20So, the value that we're passing to the
  502. 18:22float function is the value that is
  503. 18:24returned from the input function.
  504. 18:26Similarly,
  505. 18:29we call the float function over here.
  506. 18:32Now,
  507. 18:33we can change this expression to first
  508. 18:35plus second.
  509. 18:37That is another way to write this piece
  510. 18:39of code.
  511. 18:40So, type conversion is important in
  512. 18:41Python and other programming languages.
  513. 18:44There are times you need to convert the
  514. 18:45type of a variable to a different type.
  515. 18:54In this tutorial, I'm going to show you
  516. 18:55a bunch of cool things you can do with
  517. 18:56strings in Python.
  518. 18:58So, let's start by declaring a variable
  519. 19:00called course
  520. 19:01and set it to Python for
  521. 19:04beginners.
  522. 19:06Now, this string that we have over here
  523. 19:07is technically an object. An object in
  524. 19:10Python is like an object in the real
  525. 19:12world. As a metaphor, think of the
  526. 19:14remote control of your TV.
  527. 19:16This remote control is an object, and it
  528. 19:18has a bunch of capabilities. It has a
  529. 19:19bunch of buttons for turning your TV on,
  530. 19:22turning it off, changing the volume, and
  531. 19:24so on.
  532. 19:24Now, in this program, this course
  533. 19:26variable is storing a string object.
  534. 19:29This string object has a bunch of
  535. 19:30capabilities. So, if you type course.
  536. 19:33dot, you can see all the capabilities
  537. 19:36available in a string object. These are
  538. 19:38basically functions that you can call,
  539. 19:40just like the print or input functions.
  540. 19:42The difference is that the print and
  541. 19:44input functions are general-purpose
  542. 19:46functions. They don't belong to a
  543. 19:47particular object, but the functions you
  544. 19:50see over here are specific to a string.
  545. 19:52Now more accurately, we refer to these
  546. 19:54as methods. So when a function is part
  547. 19:57of an object, we refer to that function
  548. 19:59as a method. So,
  549. 20:01let's look at a few examples. Here we
  550. 20:03have a function or a method called upper
  551. 20:06and we use that to convert a string to
  552. 20:07upper case.
  553. 20:09So if we print
  554. 20:10course.upper
  555. 20:12and run this program, you can see our
  556. 20:14course in upper case. Pretty useful.
  557. 20:17Now what you need to understand here is
  558. 20:19that this upper method does not change
  559. 20:21our original string. It will return a
  560. 20:23new string. So right after this, if we
  561. 20:26print course,
  562. 20:29you can see that our course variable is
  563. 20:31not affected. So the upper method
  564. 20:33returns a new string.
  565. 20:36Now similarly, we have another method
  566. 20:38called lower for converting a string to
  567. 20:40lower case.
  568. 20:41We have a method called find to see if
  569. 20:44our string contains a character or a
  570. 20:46sequence of characters.
  571. 20:47For example, here we can pass Y and this
  572. 20:51will return the index of the first
  573. 20:53occurrence of Y in our string. So,
  574. 20:57in Python, the index of the first
  575. 20:59character in a string is zero. So here
  576. 21:01we have 0 1 2 3 4 and so on.
  577. 21:04So when we run this program, you're
  578. 21:05going to see one on the terminal because
  579. 21:07the index of Y is one. Take a look.
  580. 21:11First I'm going to delete this line. We
  581. 21:12don't need it anymore. Also, let's
  582. 21:14delete this line.
  583. 21:16Let's run the program. There you go.
  584. 21:20Now as I told you before, Python is
  585. 21:21sensitive to lower case and upper case
  586. 21:24letters. So if I pass an upper case Y
  587. 21:27here,
  588. 21:28this find method returns -1 because we
  589. 21:31don't have an upper case Y in this
  590. 21:33string.
  591. 21:34We can also pass a sequence of
  592. 21:35characters. For example, four.
  593. 21:38So this will return the index of the
  594. 21:39word four. Take a look.
  595. 21:43So it's seven.
  596. 21:45Now there are times we want to replace
  597. 21:46something in a string with something
  598. 21:47else.
  599. 21:48To do that, we use the replace method.
  600. 21:51Replace.
  601. 21:52So we can replace four with a string
  602. 21:56containing the number four. Take a look.
  603. 21:59So Python for beginners.
  604. 22:02Obviously,
  605. 22:03if you look for a character or a
  606. 22:04sequence of characters that don't exist
  607. 22:06in our string, nothing is going to
  608. 22:08happen. For example, if you try to
  609. 22:10replace X with four, obviously, we don't
  610. 22:13have X here, so nothing is going to
  611. 22:15happen.
  612. 22:16Also, just like the upper method, the
  613. 22:19replace method is not going to modify
  614. 22:21our original string. So it's going to
  615. 22:23return a new string. This is because
  616. 22:25strings in Python and many other
  617. 22:27programming languages are immutable. We
  618. 22:29cannot change them once we create them.
  619. 22:32Whenever we want to change a string,
  620. 22:33we'll end up with a new string object in
  621. 22:35memory.
  622. 22:36Now one last thing I want to cover in
  623. 22:37this tutorial. There are times you want
  624. 22:39to see if your string contains a
  625. 22:41character or a sequence of characters.
  626. 22:43One way to do that is using the find
  627. 22:45method that we talked about. So let's
  628. 22:48see if our string contains Python.
  629. 22:52Now when we run this program,
  630. 22:54that is the index of the first
  631. 22:56occurrence of the word Python in our
  632. 22:58string.
  633. 22:59Now in Python, we can also use the in
  634. 23:01operator.
  635. 23:02So we can write an expression like this.
  636. 23:05We type a string,
  637. 23:07Python,
  638. 23:08then we type in. This is a special
  639. 23:10keyword in Python. This is what we call
  640. 23:13the in operator.
  641. 23:15So after that,
  642. 23:16we type the name of our variable.
  643. 23:18So with this expression, we're checking
  644. 23:20to see if we have Python in course. As
  645. 23:23you can see, Python code is very
  646. 23:25readable. It's like plain English. So
  647. 23:27when we run this program,
  648. 23:29instead of seeing the index of the first
  649. 23:31occurrence of Python, we see a boolean
  650. 23:34value. This is more desirable in a lot
  651. 23:36of cases.
  652. 23:37Next we're going to look at arithmetic
  653. 23:38operations.
  654. 23:46In this tutorial, I'm going to show you
  655. 23:47the arithmetic operators that we have in
  656. 23:49Python. These are the same arithmetic
  657. 23:51operators that we have in math. For
  658. 23:53example, we can add numbers, we can
  659. 23:55subtract them, multiply them and so on.
  660. 23:57So let's print 10 + 3.
  661. 24:01When we run this program, we get 13.
  662. 24:03So this is the addition operator. We
  663. 24:06also have subtraction.
  664. 24:07We have multiplication
  665. 24:09and division. Now technically, we have
  666. 24:12two different types of division
  667. 24:13operators. We have a division with one
  668. 24:15slash and another with two slashes.
  669. 24:18Let's look at the differences. If you
  670. 24:20use a single slash,
  671. 24:22we get a floating point number that is a
  672. 24:24number with a decimal point. But if you
  673. 24:26use double slashes,
  674. 24:28we get
  675. 24:29an integer, a whole number.
  676. 24:32We also have the modulus operator that
  677. 24:34is indicated by a percent sign and this
  678. 24:37returns the remainder of the division of
  679. 24:3910 by 3. So that is one.
  680. 24:43And finally, we have the exponent
  681. 24:44operator that is indicated by two
  682. 24:46asterisks. So this is 10 to the power of
  683. 24:50three. So when we run this, we get
  684. 24:521,000.
  685. 24:54Now,
  686. 24:54for all these operators that you saw, we
  687. 24:56have an augmented assignment operator.
  688. 24:59Let me explain what it means. So let's
  689. 25:01say we have a variable called X
  690. 25:03and we set it to 10.
  691. 25:05Now we want to increment the value of X
  692. 25:07by three. So we have to write code like
  693. 25:09this. X
  694. 25:10= X + 3. When Python executes this code,
  695. 25:14it's going to evaluate this expression
  696. 25:16or this piece of code. The result of
  697. 25:18this expression is 10 + 3 which is 13.
  698. 25:21Then it will store 13 in the X.
  699. 25:24Now there is another way to achieve the
  700. 25:26same result using less code.
  701. 25:28We can type X + = 3.
  702. 25:32What we have on line three is exactly
  703. 25:34identical to what we have on line two.
  704. 25:36So what we have here is called the
  705. 25:38augmented assignment operator. So we
  706. 25:41have this assignment operator, but we
  707. 25:43have augmented or enhanced it.
  708. 25:46Now here we can also use
  709. 25:48subtraction to decrease the value of X
  710. 25:50by three. We can use multiplication and
  711. 25:53so on.
  712. 25:54So these are the arithmetic operators in
  713. 25:56Python.
  714. 26:03All right, let me ask you a question.
  715. 26:04I'm going to declare a variable called X
  716. 26:07and set it to 10 + 3 * 2.
  717. 26:10What do you think is the result of this
  718. 26:11expression? This is a basic math
  719. 26:13question that unfortunately, a lot of
  720. 26:15people fail to answer.
  721. 26:17The answer is 16.
  722. 26:19Here's the reason.
  723. 26:20In math, we have this concept called
  724. 26:22operator precedence and that determines
  725. 26:24the order in which these operators are
  726. 26:26applied. So multiplication and division
  727. 26:30have a higher order. So this part of the
  728. 26:32expression gets evaluated first. So 2 *
  729. 26:343 is 6 and then the result is added to
  730. 26:3810. That is why the result of this
  731. 26:40expression is 16.
  732. 26:42Now in Python, operator precedence is
  733. 26:44exactly like math, but we can always
  734. 26:46change it using parentheses. For
  735. 26:48example, in this expression, if you want
  736. 26:5010 + 3 to be evaluated first, we can
  737. 26:53wrap it in parentheses.
  738. 26:55So,
  739. 26:56like this.
  740. 26:57Now when we execute this code, we're
  741. 26:59going to see 26 because 10 + 3 is 13 and
  742. 27:03that divided by 2 is 26. Let's verify
  743. 27:05this. So print X
  744. 27:08and we get 26.
  745. 27:15So you learned about the arithmetic
  746. 27:17operators in Python. Now in Python, we
  747. 27:19have another set of operators called
  748. 27:21comparison operators. We use these
  749. 27:23operators to compare values. Let me show
  750. 27:26you. So I'm going to declare a variable
  751. 27:28called X
  752. 27:29and set it to an expression like this. 3
  753. 27:32is greater than 2. So what we have here,
  754. 27:35this piece of code, this expression is
  755. 27:37called a boolean expression because it
  756. 27:39produces a boolean value.
  757. 27:41So in this case, because 3 is greater
  758. 27:43than 2, the result of this expression is
  759. 27:46the boolean true.
  760. 27:47So if we print X,
  761. 27:51we get true on the terminal.
  762. 27:53So here is the greater than operator. We
  763. 27:55also have greater than or equal to. We
  764. 27:58have less than. We have less than or
  765. 28:01equal to.
  766. 28:02Here's the equality operator which is
  767. 28:04indicated by two equal signs. Do not
  768. 28:07confuse this with the assignment
  769. 28:08operator. So here we're comparing three
  770. 28:11and two for equality. So if we run our
  771. 28:13program,
  772. 28:15we see false because three does not
  773. 28:17equal to two.
  774. 28:19So here's the equality operator. We also
  775. 28:21have
  776. 28:22the not equality operator that is
  777. 28:24indicated by an exclamation mark
  778. 28:26followed by an equal sign.
  779. 28:29So let's quickly recap.
  780. 28:31Here are the comparison operators we
  781. 28:32have in Python.
  782. 28:34Greater than, greater than or equal to,
  783. 28:36less than, less than or equal to,
  784. 28:39equal and not equal.
  785. 28:41These operators are extremely important
  786. 28:43in real Python programs because quite
  787. 28:45often we have to compare values to
  788. 28:47evaluate certain conditions. You're
  789. 28:49going to see that soon.
  790. 28:56In Python, we have another set of
  791. 28:58operators called logical operators.
  792. 29:00We use these operators to build complex
  793. 29:02rules and conditions. Let me show you.
  794. 29:05So I'm going to declare a variable
  795. 29:06called price and set it to 25.
  796. 29:09Now let's print
  797. 29:10a boolean expression like this.
  798. 29:12Price is greater than 10.
  799. 29:15Now let's say we want to check to see if
  800. 29:17the price is between 10 and 30.
  801. 29:20This is where we use the logical and
  802. 29:22operator. So we type and and right after
  803. 29:25that, we type another boolean
  804. 29:27expression. So price less than 30.
  805. 29:31So with this and operator, if both these
  806. 29:33boolean expressions return true, the
  807. 29:36result of this entire expression will be
  808. 29:37true. Take a look. So,
  809. 29:41in this case we get true
  810. 29:43because the price is between 10 and $30.
  811. 29:46We also have
  812. 29:48the or operator. With the or operator,
  813. 29:51if at least one of these boolean
  814. 29:52expressions returns true, then the
  815. 29:55result of this entire expression will be
  816. 29:56true.
  817. 29:57To demonstrate this, I'm going to change
  818. 29:59price to five. Let's see how Python is
  819. 30:02going to execute this code. So, first
  820. 30:04it's going to look at this boolean
  821. 30:05expression. Is price greater than 10?
  822. 30:08No, it's not. So, it will keep going.
  823. 30:11Then it will look at the second boolean
  824. 30:13expression. Is price less than 30? It
  825. 30:16sure is. So, the result of this entire
  826. 30:18expression will be true. Take a look.
  827. 30:22There you go.
  828. 30:23We also have the not operator, which
  829. 30:25basically inverses any values that you
  830. 30:27give it. Let me show you. So,
  831. 30:30we're going to have one boolean
  832. 30:32expression, price greater than 10.
  833. 30:34The result of this expression is false.
  834. 30:37Now, if you apply the not operator, this
  835. 30:40will inverse false to true. So, let me
  836. 30:43run the program, we get true.
  837. 30:46So, let's quickly recap. In Python, we
  838. 30:48have three logical operators. We have
  839. 30:50logical and, which returns true if both
  840. 30:53expressions return true.
  841. 30:55We have logical or, which returns true
  842. 30:57if at least one expression returns true.
  843. 31:00And we have not, which inverses any
  844. 31:02value that we give it.
  845. 31:10In this tutorial, we're going to talk
  846. 31:11about if statements in Python. We use if
  847. 31:14statements to make decisions in our
  848. 31:15programs. For example, we can declare a
  849. 31:18variable called temperature, and
  850. 31:19depending on the value of this variable,
  851. 31:21we can print different messages on the
  852. 31:23terminal. Let me show you.
  853. 31:25So, here's our temperature variable.
  854. 31:27We set it to 35.
  855. 31:29Now, let's say if temperature is greater
  856. 31:32than 30, we want to print a message
  857. 31:34saying it's a hot day.
  858. 31:35So, we type if,
  859. 31:37then we type a condition, and this is
  860. 31:39where we use our comparison operators.
  861. 31:42So, we type temperature greater than 30.
  862. 31:46Then, so we add a colon, and see what
  863. 31:49happens when I press enter now.
  864. 31:51The caret is indented, and this
  865. 31:53represents a block of code. So, the code
  866. 31:56that we write over here will be executed
  867. 31:58if this condition is true. Otherwise,
  868. 32:00it's not going to be executed. Let me
  869. 32:02show you. So,
  870. 32:04we're going to print
  871. 32:06it's a hot day.
  872. 32:08And by the way, note that here I've
  873. 32:09surrounded the string with double
  874. 32:11quotes, because here we have a single
  875. 32:14quote as an apostrophe. So, I couldn't
  876. 32:17declare a string like this with single
  877. 32:19quotes. If I typed it's a hot day, look,
  878. 32:23Python gets confused. Because it thinks
  879. 32:26the single quote represents the end of
  880. 32:28our string, so it doesn't recognize the
  881. 32:30subsequent characters, okay? So, that's
  882. 32:32why we use
  883. 32:34double quotes here,
  884. 32:36so we can have an apostrophe in our
  885. 32:38string.
  886. 32:39So, it's a hot day.
  887. 32:41Now, if I press enter again,
  888. 32:43the caret is indented, so the code that
  889. 32:46we write here will be part of our if
  890. 32:48block, and it will get executed if this
  891. 32:50condition is true. So, here we can print
  892. 32:53a second message, drink plenty of water.
  893. 32:57Now, to terminate this block, we press
  894. 32:59enter, and then press shift and tab.
  895. 33:02The caret is no longer indented, so the
  896. 33:05code that we write here will always get
  897. 33:07executed, no matter what, whether this
  898. 33:09condition is true or not.
  899. 33:11Now, in C-based programming languages
  900. 33:13like C++, C#, Java, and JavaScript, we
  901. 33:17represent a block of code using curly
  902. 33:19braces. So,
  903. 33:21you start a block of code using a left
  904. 33:23brace, and then end it using a right
  905. 33:26brace. In Python, we don't have curly
  906. 33:28braces. So, we use indentation to
  907. 33:30represent a block of code, okay?
  908. 33:33So, in this case,
  909. 33:35these two lines are indented, and that
  910. 33:37means they are part of this block of
  911. 33:40code.
  912. 33:41Now, let's run the program and see what
  913. 33:42happens.
  914. 33:44So, we see these two messages because
  915. 33:46the temperature is greater than 30.
  916. 33:48Now,
  917. 33:49if I change the temperature to 25,
  918. 33:52and run the program again, we don't see
  919. 33:54anything, okay?
  920. 33:57Now, after this block, let's print done.
  921. 34:01Because this code is not indented, it
  922. 34:03will always get executed. It's not part
  923. 34:05of our if block, okay? So, take a look.
  924. 34:09Here's the done message.
  925. 34:11Now, let's add a second condition.
  926. 34:14So, if temperature is not greater than
  927. 34:1630, that means it's less than or equal
  928. 34:18to 30.
  929. 34:20So, I'm going to add a second condition.
  930. 34:23So, if the temperature is between 20 and
  931. 34:2530, I want to print it's a nice day.
  932. 34:28So, here we type L if, that is short for
  933. 34:31else if.
  934. 34:33And here we type a second condition.
  935. 34:35So, temperature greater than 20.
  936. 34:39We add a colon, press enter. Now, we
  937. 34:42have a new block.
  938. 34:43So, here we can print
  939. 34:45it's a nice day.
  940. 34:48So, if this condition is true, that
  941. 34:50means the temperature
  942. 34:52is greater than 20, and less than or
  943. 34:54equal to 30.
  944. 34:56Now, what we have here is called a
  945. 34:57comment. That is why it's grayed out.
  946. 35:00It's not real code, it's just some note
  947. 35:02that we add to our program. Python is
  948. 35:04not going to execute this. So, whenever
  949. 35:06we type a pound sign, what we have after
  950. 35:08is treated as a comment, okay?
  951. 35:11So, if this condition is true, then
  952. 35:13we're going to see this message on the
  953. 35:15terminal. Let's run our program and
  954. 35:17verify this.
  955. 35:19There you go. The temperature is 25,
  956. 35:21that's why we see this message.
  957. 35:23Now, we can have as many conditions as
  958. 35:25we want. There are no limitations, okay?
  959. 35:28So, let's add another condition. L if,
  960. 35:31temperature is greater than 10, then
  961. 35:35we're going to print
  962. 35:37it's a bit cold.
  963. 35:39Now, in this case, if this condition is
  964. 35:41true, that means the temperature is
  965. 35:43greater than 10,
  966. 35:45and less than or equal to 20.
  967. 35:48Now, finally, if the temperature is less
  968. 35:49than 10, let's print a message saying
  969. 35:51it's a cold day. So,
  970. 35:54here we type else, then we add a colon,
  971. 35:57and now we have a new block. So, this
  972. 36:00code will get executed if none of the
  973. 36:02above conditions are true. So, here we
  974. 36:05can print
  975. 36:07it's cold.
  976. 36:10So, this is how we use if statements to
  977. 36:12make decisions in our programs.
  978. 36:20Here's a great exercise for you to
  979. 36:21practice what you have learned so far. I
  980. 36:23want you to write a weight converter
  981. 36:25program like this.
  982. 36:27So, this program is asking me my weight.
  983. 36:29I enter 170.
  984. 36:31Next, it's asking me if the weight is in
  985. 36:34kilograms or pounds. So, I can type K
  986. 36:36for kilograms, or L for pounds. I can
  987. 36:39type a lowercase L, or an uppercase L.
  988. 36:42It doesn't matter. So, let's go with a
  989. 36:44lowercase L.
  990. 36:46Now, it tells me weight in kilogram is
  991. 36:4976.5.
  992. 36:51So, go ahead and spend 5 minutes on this
  993. 36:52exercise. You can use the comment box
  994. 36:54below to show your code with others. And
  995. 36:57then when you're done, come back and see
  996. 36:58my solution.
  997. 37:02So, first we call our input function to
  998. 37:05ask the first question. Weight.
  999. 37:08We get the result and store it in a
  1000. 37:09variable called weight.
  1001. 37:12Next, we call the input function one
  1002. 37:13more time to ask the second question.
  1003. 37:16Is this in kilogram
  1004. 37:18or
  1005. 37:19pounds?
  1006. 37:22We get the result and store it in a
  1007. 37:24variable called unit.
  1008. 37:25This is where we're going to use an if
  1009. 37:27statement.
  1010. 37:28So,
  1011. 37:29we want to check to see if unit equals
  1012. 37:32K.
  1013. 37:34Then, we should convert the weight to
  1014. 37:36pounds and print it on the terminal.
  1015. 37:38However,
  1016. 37:39with this code, if I type a lowercase K,
  1017. 37:41this condition is not going to be true,
  1018. 37:43because earlier I told you that Python
  1019. 37:45is a case-sensitive language. So,
  1020. 37:48we need to convert this string to
  1021. 37:50uppercase. Earlier we talked about
  1022. 37:52string methods. So, if you type dot, we
  1023. 37:54can see all the functions or methods
  1024. 37:56available in a string object. So, we use
  1025. 37:59the upper method,
  1026. 38:01and this returns a new string in
  1027. 38:02uppercase.
  1028. 38:04Now, if this condition is true, first we
  1029. 38:06need to convert the weight to pounds.
  1030. 38:08So, we declare a new variable called
  1031. 38:10converted,
  1032. 38:12get the weight, and divide it by 0.45.
  1033. 38:17And then we can print this on the
  1034. 38:18terminal. So,
  1035. 38:21we say weight
  1036. 38:22in pounds is
  1037. 38:24then we append converted.
  1038. 38:27Now, to terminate this block, we press
  1039. 38:29shift and tab.
  1040. 38:31Else, colon.
  1041. 38:33So, if this condition is not true, that
  1042. 38:35means the weight was entered in pounds,
  1043. 38:37so we need to convert it to kilograms.
  1044. 38:39Once again, we declare a variable
  1045. 38:41converted, and set it to weight times
  1046. 38:450.45.
  1047. 38:47And then we print weight in kilograms,
  1048. 38:52and here we concatenate
  1049. 38:54the string with converted.
  1050. 38:56All right. Now, we need to terminate
  1051. 38:58this block, so we press enter, then
  1052. 39:00shift and tab. Good.
  1053. 39:02Now, if you run this program, we're
  1054. 39:03going to see an error. Let me show you.
  1055. 39:05So, let's run it.
  1056. 39:07Here I'm going to enter 170.
  1057. 39:10Then I type a lowercase L. Okay. Here's
  1058. 39:14an error.
  1059. 39:15Can't multiply sequence by non-int of
  1060. 39:17type float. So, this is where we got
  1061. 39:20this error. When we tried to multiply
  1062. 39:23the weight by 0.45.
  1063. 39:26So,
  1064. 39:27that is line seven in our code. Now, the
  1065. 39:29reason this is happening is because the
  1066. 39:31weight variable is storing a string
  1067. 39:33object. Because earlier I told you the
  1068. 39:36input function always returns a string.
  1069. 39:39So, here we need to convert the weight
  1070. 39:41to a number.
  1071. 39:42We can either use the int or the float
  1072. 39:45function.
  1073. 39:46Let's run the program one more time.
  1074. 39:48So,
  1075. 39:49170
  1076. 39:52in pounds.
  1077. 39:53Here's the second error. Can only
  1078. 39:55concatenate string, not float to string.
  1079. 39:59And that error occurred
  1080. 40:01over here.
  1081. 40:02When we try to print the weight in
  1082. 40:04kilograms. So,
  1083. 40:06look at line seven.
  1084. 40:08In this case, weight is an integer.
  1085. 40:10We're multiplying an integer by a float,
  1086. 40:12and the result of this operation is
  1087. 40:14going to be a float.
  1088. 40:16Now, on line eight, we're trying to
  1089. 40:18concatenate or combine a string with a
  1090. 40:21float, and Python doesn't know how to
  1091. 40:23execute this code. To solve this
  1092. 40:25problem, we need to convert this float
  1093. 40:27to a string. So, here we use the
  1094. 40:29built-in str function.
  1095. 40:32Let's run the program and see what
  1096. 40:33happens.
  1097. 40:34So,
  1098. 40:35170
  1099. 40:37in pounds, and here's my weight in
  1100. 40:40kilograms. Now, let's try entering a
  1101. 40:42weight in kilograms. So, running one
  1102. 40:44more time.
  1103. 40:45Let's say 76
  1104. 40:47kilos.
  1105. 40:49Here we get a familiar error. Can only
  1106. 40:52concatenate string to string, not float.
  1107. 40:55So, this error occurred
  1108. 40:56on line five, where we try to
  1109. 40:58concatenate a string
  1110. 41:01to a float. So, once again, we need to
  1111. 41:03convert this to a string object.
  1112. 41:06Now, let's run the program one more
  1113. 41:07time.
  1114. 41:0976
  1115. 41:10kilos
  1116. 41:11is equal to 168 lbs.
  1117. 41:16Hey guys, I just wanted to let you know
  1118. 41:17that I have an online coding school at
  1119. 41:19codewithmosh.com, where you can find
  1120. 41:21plenty of courses on web and mobile
  1121. 41:22development. In fact, I have a
  1122. 41:24comprehensive Python course that teaches
  1123. 41:26you everything about Python from the
  1124. 41:28basics to more advanced concepts. So,
  1125. 41:30after you watch this tutorial, if you
  1126. 41:32want to learn more, you may want to look
  1127. 41:33at my Python course. It comes with a
  1128. 41:3530-day money-back guarantee and a
  1129. 41:36certificate of completion you can add to
  1130. 41:38your resume. In case you're interested,
  1131. 41:40the link is below this video.
  1132. 41:46In this tutorial, we're going to talk
  1133. 41:47about while loops in Python. We use
  1134. 41:50while loops to repeat a block of code
  1135. 41:52multiple times. For example, let's say
  1136. 41:54you want to print the numbers one to
  1137. 41:55five.
  1138. 41:56A poor way of doing this is writing code
  1139. 41:58like this. Print one, then print two,
  1140. 42:01print three,
  1141. 42:02print four, and print five.
  1142. 42:05Now, why is this a bad approach? Well,
  1143. 42:07what if you wanted to print the numbers
  1144. 42:09one to 1 million? We don't want to write
  1145. 42:111 million lines of code, each line
  1146. 42:13printing a number. This is where you use
  1147. 42:15while loops. So,
  1148. 42:17we start off by declaring a variable
  1149. 42:19like I, and set it to our initial
  1150. 42:21number. Let's say one.
  1151. 42:23Then we type while, and here we type a
  1152. 42:25condition. So, once again, we can use
  1153. 42:27our comparison operators.
  1154. 42:29Let's say I less than five. Less than or
  1155. 42:32equal to five.
  1156. 42:34As long as this condition is true, then
  1157. 42:36the code that we write inside of the
  1158. 42:38while block will get executed. So, here
  1159. 42:40we type a colon and press enter. Now, we
  1160. 42:43have a block of code.
  1161. 42:45In this block, we can print I,
  1162. 42:48and then we need to increment I by one.
  1163. 42:50So, we set I to I + 1. If we don't do
  1164. 42:53this, I will always be one, and this
  1165. 42:56block of code
  1166. 42:57will get executed indefinitely. It will
  1167. 43:00never terminate. Basically, our program
  1168. 43:02will continue running until it runs out
  1169. 43:04of memory.
  1170. 43:05So, in the first iteration, I is one, is
  1171. 43:08less than five, so Python is going to
  1172. 43:10execute this block of code. It will
  1173. 43:13print one on the terminal, and then I
  1174. 43:15becomes two.
  1175. 43:16Then the control moves back over here.
  1176. 43:18So, Python evaluates this condition. Two
  1177. 43:21is less than or equal to five, so the
  1178. 43:23condition is true, and once again,
  1179. 43:25Python is going to execute this block
  1180. 43:27one more time.
  1181. 43:28In the second iteration, we're going to
  1182. 43:30see two on the terminal, and then I will
  1183. 43:32become three.
  1184. 43:33So, this will continue until this
  1185. 43:35condition is no longer true. Let me show
  1186. 43:37you. So,
  1187. 43:39let's run this program.
  1188. 43:41Now, we see the numbers one to five. So,
  1189. 43:43this is the beauty of while loops.
  1190. 43:45I can easily change five
  1191. 43:48to 1
  1192. 43:49thousand. And by the way, I'm separating
  1193. 43:51these three digits using an underscore.
  1194. 43:53This makes my number more readable. We
  1195. 43:55don't have to add this, but it just
  1196. 43:56makes our code more readable.
  1197. 43:58So, let's run the program.
  1198. 44:01Now, we see the numbers one to 1
  1199. 44:02thousand. It's much easier than 1
  1200. 44:05thousand lines of code, each printing a
  1201. 44:07number. Okay?
  1202. 44:08Now, let me show you something really
  1203. 44:09cool.
  1204. 44:10So, I'm going to change this
  1205. 44:12to 10.
  1206. 44:14Now, instead of printing I, I want to
  1207. 44:17print an expression. So, I'm going to
  1208. 44:19multiply I
  1209. 44:21by a string,
  1210. 44:22an asterisk.
  1211. 44:23Now, you might be confused here, because
  1212. 44:25earlier I told you that in Python, we
  1213. 44:27cannot concatenate a number to a string.
  1214. 44:30But here I'm using the multiplication
  1215. 44:32operator. Well, this is different. We
  1216. 44:34can multiply a number by a string, and
  1217. 44:37this will repeat that string based on
  1218. 44:39the value of that number. So, if I is
  1219. 44:42one, we're going to see one asterisk. If
  1220. 44:45I is five, we're going to see five
  1221. 44:46asterisks. Let me show you. So, when we
  1222. 44:49run this program,
  1223. 44:51we see this triangle shape, because in
  1224. 44:53the first iteration, I is one, so we see
  1225. 44:56one asterisk. In every iteration, I is
  1226. 44:59incremented by one, so we see one extra
  1227. 45:02asterisk. And finally, in the last
  1228. 45:04iteration, I is 10, and we see 10
  1229. 45:07asterisks on the terminal.
  1230. 45:15So, you have learned about three types
  1231. 45:16of data in Python.
  1232. 45:18We have numbers, which can be integers
  1233. 45:20or floats. We have booleans and strings.
  1234. 45:24These are what they refer to as
  1235. 45:26primitive or basic types in Python.
  1236. 45:29Now, in Python, we have a bunch of
  1237. 45:30complex types as well. These complex
  1238. 45:32types are very useful in building real
  1239. 45:34applications.
  1240. 45:36In this tutorial, we're going to talk
  1241. 45:37about lists. We use lists whenever we
  1242. 45:40want to represent a list of objects,
  1243. 45:42like a list of numbers or a list of
  1244. 45:44names. Let me show you.
  1245. 45:45So, I'm going to declare a variable
  1246. 45:47called names,
  1247. 45:49and set it to a list of names.
  1248. 45:51So, here we add square brackets to
  1249. 45:53represent a list.
  1250. 45:54And now we can add one or more objects
  1251. 45:56inside this list. In this list, we add a
  1252. 45:58bunch of names,
  1253. 46:00like John,
  1254. 46:01Bob,
  1255. 46:03Mosh,
  1256. 46:04Sam, and Mary.
  1257. 46:06So, we separate these elements, these
  1258. 46:08items, using a comma. Okay?
  1259. 46:10Now,
  1260. 46:11let's print our list. So, print names.
  1261. 46:15Take a look.
  1262. 46:16It comes out exactly like how we wrote
  1263. 46:18it.
  1264. 46:19Now, we can also get individual elements
  1265. 46:21in this list. For example, if you want
  1266. 46:23to get the first element in this list,
  1267. 46:26here we type a pair of square brackets,
  1268. 46:28and inside the square brackets, we type
  1269. 46:30an index.
  1270. 46:32The index of the first element in this
  1271. 46:33list is zero. So, now, when we run this
  1272. 46:36program, we're going to see John on the
  1273. 46:38terminal.
  1274. 46:39There you go.
  1275. 46:40Now, in Python, we can also use a
  1276. 46:43negative index. This is a feature that I
  1277. 46:45personally have not seen in other
  1278. 46:47programming languages.
  1279. 46:48So, if zero represents the first element
  1280. 46:52in this list, what do you think negative
  1281. 46:54one represents?
  1282. 46:56It represents the last element in this
  1283. 46:58list. Let's verify it. So, let's run the
  1284. 47:01program, and here's Mary.
  1285. 47:03What about
  1286. 47:06negative two? Well, that represents the
  1287. 47:09second element
  1288. 47:10from the end of the list.
  1289. 47:12So, let's run the program, and here's
  1290. 47:14Sam.
  1291. 47:15Now, we can also change an object at a
  1292. 47:17given index. For example, let's say here
  1293. 47:20we made a mistake, and this John should
  1294. 47:22not be spelled with an H. So, we need to
  1295. 47:25reset it. We type names of zero.
  1296. 47:29Now, we treat this like a regular
  1297. 47:30variable. So, we set it to a new value.
  1298. 47:33We set it to John without an H. Now,
  1299. 47:36let's print our list.
  1300. 47:38And here's our updated list. Beautiful.
  1301. 47:41We can also select a range of values.
  1302. 47:44For example, let's say we're only
  1303. 47:45interested in the first three names.
  1304. 47:49So, over here, we type square brackets,
  1305. 47:51and here we need to type two indexes, a
  1306. 47:54start index and an end index.
  1307. 47:56Our start index is zero, because we want
  1308. 47:58to start from here,
  1309. 48:00and our end index is going to be zero,
  1310. 48:03one, two, plus one, that's going to be
  1311. 48:06three. So, we add a colon, three.
  1312. 48:10So, Python is going to return all the
  1313. 48:11elements from the start index up to the
  1314. 48:14end index, but excluding the end index.
  1315. 48:17So, it's going to return the elements at
  1316. 48:19index zero, one, and two. Take a look.
  1317. 48:24Here are the first three names.
  1318. 48:27And by the way,
  1319. 48:29this expression does not modify our
  1320. 48:31original list. It returns a new list.
  1321. 48:34So, right after this print statement, if
  1322. 48:37we print our original list,
  1323. 48:40we can see that it's not changed.
  1324. 48:42So, this is how we use lists in Python.
  1325. 48:51Earlier, I told you that strings in
  1326. 48:52Python are objects. Objects in
  1327. 48:55programming are kind of like objects in
  1328. 48:57the real world, like your mobile phone,
  1329. 48:59your bicycle, the remote control of your
  1330. 49:01TV, and so on. They have certain
  1331. 49:02capabilities. So, if you type a string
  1332. 49:05here,
  1333. 49:06and then press dot, we can see all the
  1334. 49:08functions or methods available in a
  1335. 49:10string object in Python.
  1336. 49:12Now, lists are also objects, so they
  1337. 49:15have a bunch of methods for adding
  1338. 49:16items, for removing them, and so on. Let
  1339. 49:18me show you. So, I'm going to declare
  1340. 49:21a list of numbers.
  1341. 49:22Let's say one, two, three, four, and
  1342. 49:24five.
  1343. 49:25Now, to add a new element at the end of
  1344. 49:27this list, we can use the append method.
  1345. 49:30So, we type numbers
  1346. 49:31dot append,
  1347. 49:33and here we type six.
  1348. 49:35Now, let's print our list.
  1349. 49:39So, here's our updated list. Beautiful.
  1350. 49:42Now, what if you want to insert a number
  1351. 49:43somewhere in the middle or at the
  1352. 49:45beginning?
  1353. 49:46For that, we use the insert method.
  1354. 49:49So, we're going to call the insert
  1355. 49:51method.
  1356. 49:52Now, on the top, go to the view menu,
  1357. 49:55and look at parameter info. Look at the
  1358. 49:58shortcut. On a Mac computer, it's
  1359. 50:01command and P.
  1360. 50:02On Windows, it's probably control and P.
  1361. 50:05If we use this shortcut, we can see the
  1362. 50:07values that this method expects.
  1363. 50:10So, the first value that this method
  1364. 50:12expects is an index value, and the type
  1365. 50:15of this value is an integer.
  1366. 50:16So, if you want to insert a value at the
  1367. 50:18beginning of this list, I should pass
  1368. 50:20zero as the index of the first element,
  1369. 50:23right? So, let's pass zero.
  1370. 50:25Now,
  1371. 50:27the second value is highlighted. So, the
  1372. 50:29second value is an object, and the type
  1373. 50:31of this is T. That basically means this
  1374. 50:34can be any type. We can pass a number,
  1375. 50:36we can pass a boolean, we can pass a
  1376. 50:38string, we can pass a list or any type
  1377. 50:40of objects in Python.
  1378. 50:42So, I'm going to pass -1.
  1379. 50:45Now, let's run our program.
  1380. 50:47We can see -1 appeared at the beginning
  1381. 50:49of our list.
  1382. 50:51We also have a method for removing
  1383. 50:52items. So,
  1384. 50:54let's call remove
  1385. 50:56three.
  1386. 50:57Let's run the program.
  1387. 50:59Three is gone. We only have 1 2 4 and 5.
  1388. 51:03Now, if you want to remove all the items
  1389. 51:04in the list, we call the clear method.
  1390. 51:06So, clear.
  1391. 51:09This method doesn't expect any values.
  1392. 51:11So, let's run our program. Our list is
  1393. 51:13empty.
  1394. 51:14Now, sometimes you want to know if a
  1395. 51:16given item exists in our list or not. To
  1396. 51:18do that, we use the in operator. So,
  1397. 51:21let's remove this line.
  1398. 51:24Instead of printing our numbers list,
  1399. 51:25I'm going to print an expression.
  1400. 51:28One
  1401. 51:29in numbers.
  1402. 51:31So, here we're using the in operator.
  1403. 51:33We're checking to see if one is in the
  1404. 51:35numbers list. So, this is a boolean
  1405. 51:37expression. It returns a boolean value.
  1406. 51:40Take a look.
  1407. 51:41So, we get true.
  1408. 51:43Obviously, if we search for a value that
  1409. 51:45doesn't exist in this list, like 10,
  1410. 51:48we get false, okay?
  1411. 51:51Now, finally, there are times you want
  1412. 51:52to know how many items you have in a
  1413. 51:53list. To do that, you can use the
  1414. 51:55built-in len function. So, let's print
  1415. 52:00len of numbers. So, len is a built-in
  1416. 52:04function, just like the print function.
  1417. 52:06That is why it's highlighted as purple.
  1418. 52:08It returns the number of elements in a
  1419. 52:10list. Take a look.
  1420. 52:13So, we have five elements in this list.
  1421. 52:20When writing Python programs, there are
  1422. 52:22times you want to iterate over a list
  1423. 52:25and access each item individually. Let
  1424. 52:27me show you.
  1425. 52:28So, I'm going to declare
  1426. 52:29a list of numbers,
  1427. 52:311 2 3 4 5.
  1428. 52:33Now, if we print this list,
  1429. 52:36it comes out exactly like how we wrote
  1430. 52:38it, using the square bracket notation.
  1431. 52:41But, what if we wanted to print each
  1432. 52:42item on a separate line?
  1433. 52:44That is where we use the for loop. Let
  1434. 52:46me show you. So, we're not going to
  1435. 52:48print the entire list.
  1436. 52:50Instead, we're going to type for.
  1437. 52:52Now, we declare a variable, which is
  1438. 52:53called a loop variable.
  1439. 52:56Let's call it item.
  1440. 52:57Then, we type in
  1441. 52:59numbers.
  1442. 53:00Next, we add a colon to start a block of
  1443. 53:03code. So, this is what we call a for
  1444. 53:05loop.
  1445. 53:06With this for loop, we can iterate over
  1446. 53:08all the items in this list. In each
  1447. 53:11iteration, this item variable is going
  1448. 53:13to hold one value. So, in the first
  1449. 53:15iteration, item is going to be equal to
  1450. 53:17one, in the second iteration is going to
  1451. 53:19be equal to two, and so on.
  1452. 53:21So,
  1453. 53:22now if you print item
  1454. 53:25and run our program,
  1455. 53:26we see each item on a new line.
  1456. 53:28So, this is how we use a for loop. Now,
  1457. 53:31we could also achieve the same thing
  1458. 53:32using a while loop, but our code would
  1459. 53:34be a little bit longer. Let me show you.
  1460. 53:37So, we would have to start by declaring
  1461. 53:39a loop variable outside of our while
  1462. 53:41loop. Let's say I. We set it to zero.
  1463. 53:45Now, we say while I is less than
  1464. 53:48Here, we need to find out how many items
  1465. 53:49we have in this list.
  1466. 53:51So, we use the len function.
  1467. 53:53Len of numbers.
  1468. 53:55So, as long as I is less than the length
  1469. 53:58of the list,
  1470. 53:59print
  1471. 54:00Now, we can use the square bracket
  1472. 54:02notation to get the element at this
  1473. 54:04index. So,
  1474. 54:06numbers of I.
  1475. 54:10Now, we need to increment I by one. So,
  1476. 54:13I equals I + 1.
  1477. 54:15Let's run the program and see what we
  1478. 54:16get.
  1479. 54:19So, we get the numbers 1 to 5. These are
  1480. 54:21coming out from our for loop. And then,
  1481. 54:24we get the numbers 1 to 5 one more time.
  1482. 54:26These are coming out from our while
  1483. 54:27loop. Now, if you compare these two
  1484. 54:29approaches, you can definitely see that
  1485. 54:31the implementation using the for loop is
  1486. 54:33shorter and easier to understand. We
  1487. 54:35don't have to use the square bracket
  1488. 54:37notation. We don't have to call the len
  1489. 54:39function. We don't have to declare a
  1490. 54:41variable, a loop variable, and then
  1491. 54:43increment it explicitly. So, with the
  1492. 54:46for loop, in each iteration, the item
  1493. 54:48variable will automatically hold one
  1494. 54:50value in this list.
  1495. 54:58In this tutorial, we're going to talk
  1496. 55:00about the range function in Python. We
  1497. 55:02use the range function to generate a
  1498. 55:04sequence of numbers. Let me show you.
  1499. 55:06So, we type range.
  1500. 55:08This is a built-in function, just like
  1501. 55:10the print and input functions.
  1502. 55:12Here, we can pass a value like five,
  1503. 55:14and this will return a range object. A
  1504. 55:17range object is an object that can store
  1505. 55:19a sequence of numbers. Let me show you.
  1506. 55:22So,
  1507. 55:23let's call that numbers.
  1508. 55:25So, this is a range object. In this
  1509. 55:27object, we're going to have the numbers
  1510. 55:29zero to five, but excluding five.
  1511. 55:32Now, if we print
  1512. 55:34numbers,
  1513. 55:37we're going to see this message, range
  1514. 55:39of zero to five, not the actual numbers,
  1515. 55:41because this is the default
  1516. 55:42representation of a range object. To see
  1517. 55:45the actual numbers, we need to iterate
  1518. 55:47over this range object using a for loop.
  1519. 55:50In the last video, you learned how to
  1520. 55:51iterate over a list using a for loop,
  1521. 55:53but we can also iterate over a range
  1522. 55:55object using a for loop. Basically, we
  1523. 55:57can use the for loop with any object
  1524. 56:00that represents a sequence of objects.
  1525. 56:02So, instead of printing numbers,
  1526. 56:05we're going to use a for loop.
  1527. 56:07For number
  1528. 56:08in numbers,
  1529. 56:10colon,
  1530. 56:11we're going to print
  1531. 56:13number.
  1532. 56:15Take a look.
  1533. 56:17Now, we see the numbers zero to four.
  1534. 56:20So,
  1535. 56:21range of five
  1536. 56:22generates a sequence of numbers starting
  1537. 56:24from zero up to the number we specify
  1538. 56:27here.
  1539. 56:28Now, if we supply two values,
  1540. 56:30the first value is going to be
  1541. 56:32considered the starting value,
  1542. 56:34and the second value is going to be
  1543. 56:36considered the ending value, and it's
  1544. 56:37going to be excluded. So, range of five
  1545. 56:40to 10 is going to generate the numbers
  1546. 56:42five to nine. Take a look.
  1547. 56:45There you go.
  1548. 56:46Now, we can also supply a third value,
  1549. 56:49and that will be used as a step. So,
  1550. 56:51let's say instead of having a sequence
  1551. 56:53of numbers like 5 6 7 8 9, we want to
  1552. 56:56jump two numbers at a time. So, 5 7 9,
  1553. 56:59and so on.
  1554. 57:00So, here we pass two as the step. Take a
  1555. 57:03look.
  1556. 57:05So, we get these odd numbers, 5 7 and 9.
  1557. 57:10So, this is the range function in
  1558. 57:11Python. Like quite often, you see the
  1559. 57:13range function used as part of a for
  1560. 57:15loop, because we don't really need to
  1561. 57:17store the result in a separate variable.
  1562. 57:19We can call the range function right
  1563. 57:21here, where we are using the numbers
  1564. 57:23variable. So, we can type range of five,
  1565. 57:27and this will return a range object
  1566. 57:29holding the numbers zero to four. Take a
  1567. 57:31look.
  1568. 57:32There you go.
  1569. 57:34So, we don't really need to store the
  1570. 57:35result in a separate variable like
  1571. 57:37numbers.
  1572. 57:39There you go.
  1573. 57:47In this tutorial, we're going to talk
  1574. 57:48about tuples in Python.
  1575. 57:50Tuples are kind of like lists. We use
  1576. 57:52them to store a sequence of objects, but
  1577. 57:54tuples are immutable, which means we
  1578. 57:57cannot change them once we create them.
  1579. 57:59Let me show you.
  1580. 58:00So, I'm going to start by defining a
  1581. 58:02list of numbers, 1 2 3.
  1582. 58:04Now, we use square brackets to define a
  1583. 58:06list,
  1584. 58:07and parentheses to define a tuple. So,
  1585. 58:10now this numbers variable is storing a
  1586. 58:12tuple.
  1587. 58:13If we try
  1588. 58:15to reassign, let's say the first
  1589. 58:16element,
  1590. 58:18we're going to get an error.
  1591. 58:20Tuple object does not support item
  1592. 58:22assignment.
  1593. 58:23So, this is what I meant by tuples are
  1594. 58:25immutable. They're unchangeable.
  1595. 58:28Also,
  1596. 58:30if we type numbers {dot}
  1597. 58:32you don't see any methods like append,
  1598. 58:34insert, remove, and so on. We only have
  1599. 58:37count and index. Count returns the
  1600. 58:40number of occurrences of an element. For
  1601. 58:42example,
  1602. 58:43if we have, let's say, two threes in
  1603. 58:46this tuple,
  1604. 58:47and call count of three, this will
  1605. 58:49return two.
  1606. 58:51The other method we have here is index,
  1607. 58:53and this returns the index of the first
  1608. 58:56occurrence of the given element.
  1609. 58:58Now,
  1610. 58:59these other methods you see here that
  1611. 59:00start with an underscore, they're called
  1612. 59:03magic methods. It's an advanced topic,
  1613. 59:05and I've covered it in detail in my
  1614. 59:06complete Python programming course. If
  1615. 59:09you're interested, the link is down
  1616. 59:11below this video.
  1617. 59:12So, tuples are immutable. We cannot
  1618. 59:14change them once we create them.
  1619. 59:16Now, practically speaking, most of the
  1620. 59:18time you would be using lists, but there
  1621. 59:20are times that once you create a list of
  1622. 59:21objects, you want to make sure that
  1623. 59:23somewhere in your program, you or
  1624. 59:25someone else is not going to
  1625. 59:26accidentally modify that list. If that's
  1626. 59:29the case, then you should use a tuple.
  1627. 59:33Hey guys, I just wanted to let you know
  1628. 59:34that I have an online coding school at
  1629. 59:36codewithmosh.com where you can find
  1630. 59:38plenty of courses on web and mobile
  1631. 59:40development. In fact, I have a
  1632. 59:41comprehensive Python course that teaches
  1633. 59:44you everything about Python from the
  1634. 59:45basics to more advanced concepts. So,
  1635. 59:47after you watch this tutorial, if you
  1636. 59:49want to learn more, you may want to look
  1637. 59:50at my Python course. It comes with a
  1638. 59:5230-day money-back guarantee and a
  1639. 59:54certificate of completion you can add to
  1640. 59:55your resume. In case you're interested,
  1641. 59:57the link is below this video.
  1642. 1:00:01Hey Mosh, hi Mosh.

About this transcript

This page contains the full transcript of Python for Beginners - Learn Coding with Python in 1 Hour by Programming with Mosh, generated from the public captions YouTube serves with the video. The transcript has 9,840 words across 1,642 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.

What you can do with it

Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to translate the transcript into 100+ languages.

Free YouTube transcript tool

YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, translation to 100+ languages, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.