f-strings, Underscores, and Types2:53 with Kenneth Love
Two of Python 3.6's new features make your code easier to read and write. Another great one lets you specify the type you'd like variables to be (don't worry, we're still duck typed!) so people get the most out of your code.
The edition that I think I'm the most excited about 0:00 is the new format in string literals. 0:03 You've probably use this string method format to put some external data into 0:04 a string, right? 0:08 Something like this, name = "Kenneth" and then print, 0:09 "Hello, my name is, and then a placeholder, and then format name, right? 0:14 You've done something like this, and this works. 0:21 And it also works for simple Python statements, so like 2 + 2 = something, 0:23 we format that with 2 + 2, and we get 2 + 2 = 4. 0:30 So this is such a common scenario that they've put a shortcut into the language. 0:34 So now you can skip the entire format call. 0:40 And you can use the new F marker for strings, 0:42 kinda like how we use R for raw strings. 0:45 So we would do print(f"Hello, my name is name"), 0:48 and we get hello, my name is Kenneth. 0:54 So it brings the name variable that's in the current scope, 0:58 which was defined up here, and it puts it into the string. 1:01 We can also do this with simple statements like before, so print f"2 + 2 = 2 + 2 and 1:05 we get 2 + 2 = 4, and while we're talking about presentational things, 1:11 if you regularly have to code large numbers, 1:16 the addition of underscores to numbers makes them a little easier. 1:19 So you can do like 1. 1:24 So a 1_000_000 times 10. 1:27 And you get the new number. 1:29 Python will just ignore the underscores and sees this as a million times 10. 1:32 This changes purely cosmetic, but 1:36 it will definitely help anyone who has to type out long numbers. 1:38 Granted, that's not something you have to do very often, but 1:41 if you're doing scientific Python, if you're doing like stuff at NumPy or 1:43 SciPy, you may find this more useful. 1:46 I do believe it's actually been in those for a little while. 1:49 If you've been using Python 3's function annotation abilities or 1:52 Python 3.5's typing module, 1:55 you'll be happy to know that variable annotation has come to Python. 1:57 We swap over here to PyCharm, and we'll talk about this. 2:00 So now, if you have a type checker set up like the one that's installed by default 2:03 in PyCharm, you can annotate variables as being of a particular type, and 2:07 then you get some handy user information when you want to use them later. 2:10 So like we can say that age is an int, and 2:14 then we can set it to a default value of like 35, and so now if I do like age., and 2:17 I've got my normal age strings. 2:22 Now, PyCharm can infer that from the variable that was set before but you'll 2:27 also see things like, if you're trying to use this variable and you're trying to 2:31 use it in a way that's not compatible with an int, you can get error messages. 2:34 All of these changes, well, minor, definitely make working with strings and 2:39 big numbers nicer in Python. 2:42 And I've already found myself using the f-strings constantly, 2:44 I'm sure you will too. 2:47 The variable typing will definitely make a big impact on larger code bases too. 2:49
You need to sign up for Treehouse in order to download course files.Sign up