Getting started with Python

Getting started with Python


Published at - Sep 17, 2021

>>> It means Python is ready and can install the command. The basic idea is very simple: enter the command, press Enter, enter another command, press Enter, and continue.

You probably don't know any Python commands yet. Let's see what happens when we type something and press Enter.

>>> hello
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'hello' is not defined
>>>

Oops! That didn't work. But as I wrote in the introduction, error messages are our friends. This error message tells us what's wrong and where, and we'll learn what it means "the word 'hello' means later.

Maybe we can press Enter without typing anything?

>>>
>>>
>>>
>>>

That worked. How about numbers?

>>> 123
123
>>> -123
-123
>>> 3.14
3.14
>>> -12.3
-12.3
>>>

Wherever we go, it echoes again.

In some countries, decimal numbers are written with commas, such as 3,14 instead of 3.14. Maybe Python knows that?

>>> 3,14
(3, 14)
>>>

We did not find the error ... but (3, 14) is not what we expected! So from now on, let's use a dot with decimal numbers because 3.14 worked well. Later we will learn what it is (3, 14).

Comments

Comments are idle text when launched. They can be created by typing # and then text in the background, and they are useful when our code can be hard to understand without them.

>>> 1 + 2 # can you guess what the result is?
3
>>>

Again, I put a space after the # and multiple spaces before it just to make things easier to read.

If we write a comment on a line with no code on it, the prompt changes from >>> to .... To be honest, I have no idea why it does that and I think it would be better if it would just stay as >>>. The prompt goes back to >>> when we press Enter again.

>>> # hello there
...
>>>

Strings

Strings are small pieces of text that we can use in our programs. We can create strings by simply writing some text in quotes.

>>> 'hello'
'hello'
>>> 'this is a test'
'this is a test'
>>> 

Strings can also be written with "double quotes" instead of 'single quotes'. This is useful when we need to put quotes inside the string.

>>> "hello there"
'hello there'
>>> "it's sunny"
"it's sunny"
>>> 

It's also possible to add single quotes and double quotes into the same string, but most of the time we don't need to do that so I'm not going to talk about it now.

It doesn't matter which quotes you use when the string doesn't need to contain any quotes. If you think that one of the quote types looks nicer than the other or you find it faster to type, go ahead and use that.

Strings can be joined together easily with + or repeated with *:

>>> "hello" + "world"
'helloworld'
>>> "hello" * 3
'hellohellohello'
>>> 

Note that a # inside a string doesn't create a comment.

>>> "strings can contain # characters"
'strings can contain # characters'
>>> 

Using Python as a calculator

---------- WARNING: This part contains boring math. Proceed with caution. ----------

Let's type some math stuff into Python and see what it does.

>>> 17 + 3
20
>>> 17 - 3
14
>>> 17 * 3
51
>>> 17 / 3
5.666666666666667
>>>

It's working, Python just calculates the result and echoes it back.

I added a space on both sides of +, -, * and /. Everything would work without those spaces too:

>>> 4 + 2 + 1
7
>>> 4+2+1
7
>>>

However, I recommend always adding the spaces because they make the code easier to read.

Things are calculated in the same order as in math. The parentheses ( and ) also work the same way.

>>> 1 + 2 * 3 # 2 * 3 is calculated first
7
>>> (1 + 2) * 3 # 1 + 2 is calculated first
9
>>>

You can also leave out spaces to show what's calculated first. Python ignores it, but our code will be easier to read for people.

>>> 1 + 2*3 # now it looks like 2*3 is calculated first
7
>>>

Python also supports many other kinds of calculations, but most of the time you don't need them. Actually, you don't need even these calculations most of the time, but these calculations are probably enough when you need to calculate something.

Summary

  • Error messages are our friends.
  • We can enter any Python commands to the interactive >>> prompt, and it will echo back the result.
  • +, -, * and / work in Python just like in math.
  • Pieces of text starting with a # are comments and pieces of text in quotes are strings.
  • You can use single quotes and double quotes however you want.




About author

Harendra
Harendra Kanojiya

Hello, I am Harendra Kumar Kanojiya - Owner of this website and a Fullstack web developer. I have expertise in full-stack web development using Angular, PHP, Node JS, Python, Laravel, Codeigniter and, Other web technologies. I also love to write blogs on the latest web technology to keep me and others updated. Thank you for reading the articles.



Related Posts -

Python interview questions 2021
Python interview questions ...

Hello everyone, today in the post I am going to talk about some basic quest...





Python gui desktop app to download youtube video - python coding
Python gui desktop app to d ...

Hello Guys, Today I am going to create a youtube downloader GUI app to down...



Python program to send mail using gmail smtp - python coding
Python program to send mail ...

In this lesson on python coding, we will learn how to send mail using Gmail...





Follow Us

Follow us on facebook Click Here

Facebook QR
Scan from mobile
Join our telegram channel Click Here
Telegram QR
Scan from mobile