DEV Community

Kartik Patel
Kartik Patel

Posted on Edited on

Python for Class 10

In the Class 10 Artificial Intelligence syllabus, Python has been
introduced as a programming language. And it's obvious that a lot of
students might not understand why they're learning it, or where to even
begin.

Now, as a number-one Python hater, I didn't want to make this tutorial
at all. But a request from a beloved friend has brought me here.

I'll try to cover the relevant syllabus topics from the absolute basics.
This first post is going to be long, so grab a snack and bear with me.

Before we start, here's the plan: we'll build up the basics first, then
write actual programs. The syllabus includes programs involving lists
and concatenation, but expecting someone who has never touched a PC to
jump straight into those is... ambitious. We'll get there.

So, what is Python?

Now, one might say...

It's a snake.

And you know what? Yes, it is.

I'm serious. Python is also the name of a snake. But in this tutorial,
we're talking about Python the programming language.

Think of Python as a way to give instructions to a computer. You write
instructions in Python, and the Python interpreter reads and executes
them.

Before I overcomplicate things, let's establish a few terms. Think of
these as some bro-code definitions between you and me.

Python

A programming language used to give instructions to a computer.

Data

Information. A name, a number, a score, or pretty much anything a
program needs to work with.

Data type

Data comes in different forms. A number and a sentence aren't the same
kind of information, right? Data types tell Python what kind of value
it's dealing with.

We'll look at the important types in a moment.

Function

Okay, this part needs a little attention, so stay with me.

Imagine your program is doing a task. Instead of writing the same
instructions again and again, you can group some instructions together
and give that group a name.

That named, reusable block of code is called a function.

You can ask Python to run it whenever you need it. Asking Python to run
a function is called a function call.

Here's a small example:

def add(x, y):
    return x + y
Enter fullscreen mode Exit fullscreen mode

Don't worry. We'll break it down.

What is def?

def is a keyword.

A keyword is a word that Python has reserved for a specific purpose. You
can't use it as an ordinary variable or function name.

For example, Python has keywords such as:

  • def
  • return
  • if
  • else
  • for
  • while

Why reserve these words?

Imagine if Python had no special words or rules. It wouldn't know
whether if was meant to start a condition or was just some random name
you had written.

Keywords give Python a consistent structure for understanding your code.

Now, def is short for define.

So this:

def add(x, y):
Enter fullscreen mode Exit fullscreen mode

basically tells Python:

"Define a function named add that can receive two values."

What is add? Well... add. Addition. I named it add.

You can choose another valid name:

def kartik(x, y):
    return x + y
Enter fullscreen mode Exit fullscreen mode

That works too. The function's name is your choice, as long as it
follows Python's naming rules and isn't a reserved keyword.

One correction to a common misconception: defining two functions with
the same name doesn't necessarily produce an error in Python. The later
definition replaces the earlier name binding. So, don't reuse names
unless you understand what you're doing.

What are x and y?

They're parameters.

A parameter is a variable listed in a function definition. It receives a
value when the function is called.

For example:

def add(x, y):
    return x + y
Enter fullscreen mode Exit fullscreen mode

Here, x and y are parameters. They act like temporary names for the
values given to the function.

What is ()?

These are parentheses.

In a function definition, they contain the parameters. In a function
call, they contain the values (also called arguments) that you pass to
the function.

For example:

add(5, 3)
Enter fullscreen mode Exit fullscreen mode

Here, 5 and 3 are arguments.

What is :?

The colon marks the beginning of the function's code block.

Python uses indentation (the spaces at the start of a line) to show
which instructions belong inside that block.

For example:

def add(x, y):
    return x + y
Enter fullscreen mode Exit fullscreen mode

The return line is indented, so it belongs to the function.

What does return do?

return sends a result back from the function.

Let's actually call our function:

def add(x, y):
    return x + y

answer = add(5, 3)
print(answer)
Enter fullscreen mode Exit fullscreen mode

Output:

8
Enter fullscreen mode Exit fullscreen mode

The function receives 5 and 3, adds them, and returns 8. We store
that result in answer, then print() displays it.

And that's your first tiny Python function. Not so scary, right?

Variables: the containers of your code

Now, I assume you know what a variable is because it's also a concept
you encounter in maths.

But if you don't, imagine a labelled container that stores a value.

For example:

name = "Kartik"
age = 15
Enter fullscreen mode Exit fullscreen mode

Here, name and age are variables.

  • name stores the text "Kartik".
  • age stores the number 15.

The = sign assigns a value to a variable. It doesn't mean the same
thing as mathematical equality in an equation.

Basic data types

Let's look at the data types you'll commonly use in beginner Python
programs.

1. Integer (int)

Whole numbers, without a decimal point.

age = 15
marks = 95
temperature = -4
Enter fullscreen mode Exit fullscreen mode

2. String (str)

Text. A string is written inside quotation marks.

name = "KARTIK"
friend = "ISHANT"
message = "HEY BRO, DON'T SCROLL. STUDY PYTHON!"
Enter fullscreen mode Exit fullscreen mode

You can use either single quotes or double quotes:

'Hello'
"Hello"
Enter fullscreen mode Exit fullscreen mode

Both create strings.

3. Boolean (bool)

A Boolean has only two possible values:

True
False
Enter fullscreen mode Exit fullscreen mode

Notice the capital letters. Python is case-sensitive, so true and
false are not the Boolean values.

4. Float (float)

Numbers with a decimal point.

pi = 3.14
height = 163.5
Enter fullscreen mode Exit fullscreen mode

5. List (list)

A list stores multiple values in one variable.

marks = [85, 90, 95]
names = ["Kartik", "Ishant", "Yuvraj"]
Enter fullscreen mode Exit fullscreen mode

We'll explore lists properly in a later part, because they're important
for the syllabus.

String vs Integer: the difference

You know these quotation marks?

" "
Enter fullscreen mode Exit fullscreen mode

Anything written inside quotation marks is treated as a string.

Let's test the difference:

"23"       # String
23         # Integer

"True"     # String
True       # Boolean
Enter fullscreen mode Exit fullscreen mode

They might look similar to us, but Python treats them differently.

Here's a simple example:

print("23" + "23")
Enter fullscreen mode Exit fullscreen mode

Output:

2323
Enter fullscreen mode Exit fullscreen mode

Why?

Because both values are strings. The + operator joins them together.
This is called concatenation.

Now try:

print(23 + 23)
Enter fullscreen mode Exit fullscreen mode

Output:

46
Enter fullscreen mode Exit fullscreen mode

Because both values are integers, Python performs addition.

Same-looking symbols, different data types, different behaviour. That's
why understanding data types matters.

A quick recap

Before we end this part, let's revise what we learned:

Term Meaning


Python A programming language
Data Information used by a program
Data type The kind of value stored
Function A reusable block of code
def Keyword used to define a function
Parameter A variable listed in a function definition
Argument A value passed during a function call
Variable A name that refers to a value
return Sends a result back from a function
int Whole number
str Text
bool True or False
float Decimal number
list A collection of values

Where do we write Python code?

...

...

...

Literally, let's do that in the next part. I'm tired right now lol.

In Part 2, we'll set up a place to write Python and finally make some
actual programs. We'll work towards the syllabus exercise involving the
addition and concatenation of elements from two lists.

Bye. See you in the next part!

--- Kartik Patel

Top comments (0)