Loading video player…

Identifying the Basics of Python Syntax

00:00 Something that makes Python a great programming language to learn is that its syntax is designed to be human-readable and beginner-friendly. In this lesson, you’ll explore some fundamental concepts: comments, variables, keywords, and built-in data types.

00:14 Starting with comments, comments are notes for humans reading your code. For example, this code snippet adds tax to a price, calculating a total. So it’s preceded by a comment stating “Calculate the total price.”

00:26 Comments start with a hash or pound symbol. Since they’re meant to be notes, Python ignores comments when running your code. You should use comments to add context or explanations, but they should be relatively brief.

00:40 Variables are names that point to values. They’re a way to hold onto data and retrieve it later. You use the equals sign. The variable name goes on the left and the value to assign goes on the right.

00:50 So in the REPL, you could type language = "Python" in quotes or version = 3.13. And language would be evaluated as the string "Python".

01:00 More on strings in a bit. The rule for variable names is that they can contain letters, numbers, and underscores, but they must start with a letter or underscore, not a number.

01:11 They also can’t be Python keywords.

01:14 What are keywords? Keywords are reserved words that define Python’s syntax and structure. Their meaning and use are baked into the language, which makes them distinct from variables, which are simply words associated with values.

01:28 Keywords do things like control program flow, define reusable code, and handle errors. They represent special values and operations. Because they have this special role, they can’t be used as variable names.

01:42 If you call the help() function in the REPL and pass in the string "keywords", you’ll get a helpful list of Python keywords, seen here.

01:49 As of Python version 3.13, there are 35 keywords.

01:54 Something else you should know about Python is that everything is an object, and every object has a data type. An object’s type determines what kind of information it represents, which operations you can perform on it, and how Python stores and works with it.

02:09 Python provides several built-in data types that you always have access to. Here’s a sample of some of Python’s built-in data types. ints are whole numbers.

02:19 floats are numbers with decimals. Strings contain text. bools, or Booleans, hold true-false values. And lists, dicts, and sets are all collection types, types that hold other objects.

02:32 For numbers, Python provides several numeric data types, most important of which are integers, which represent whole numbers, and floating-point numbers, which represent decimal numbers.

02:43 You can use arithmetic operators like plus, minus, star for multiplication, and slash for division. For example, assign apples, the integer 12, and price, the float, 2.99, multiply them together, and the result is 35.88.

03:02 Booleans represent one of two truth values, True or False. Comparisons between Python objects produce Boolean results. Say you set limit to 10 and value to 9.

03:11 You can set over_limit to the result of asking the question value greater than limit, which evaluates to False since 9 is less than 10.

03:19 And for good measure, you can compare if limit is equal to 10 with a double equal sign, resulting in True.

03:27 Strings are used to represent text. Strings can use single, double, or even triple quotes. String methods like .lower() transform text, while the built-in len() function counts its characters.

03:39 F-strings insert values into text using curly bracket placeholders. You could assign name, the string "Ada", and language, the string "Python", and then the f-string, name in brackets, is learning language in brackets, would evaluate to 'Ada is learning Python'.

03:56 Calling language.lower() makes "Python" lowercase, and passing language to the len() function returns 6, the number of characters in "Python".

04:07 Lists store multiple values in order. Lists use square brackets with items separated by commas. These items can be accessed by index, also using square brackets, and starting at zero.

04:18 And lists can grow and change dynamically. So if you set numbers to the list 10, 20, 30, you can access the first element of numbers at 0, returning 10, and you can add to numbers by calling numbers.append(), passing in 40, resulting in numbers containing 10, 20, 30, 40.

04:38 Since this only just scratches the surface of these concepts, here are a few recommended courses to follow up. Variables in Python, Exploring Keywords in Python, and Exploring Basic Data Types in Python, each of which dives deep on one of the topics in this lesson.

04:54 Next up, you’ll look at tools to logically piece together more complex programs.

Become a Member to join the conversation.