PYTHON Basics In Just Few Minutes, #Simple

Here you can learn some basics of python including Creating new file, saving a file, run the code, In addition variables, string; using IDLE:

1. CREATE NEW PYTHON FILE

Create new file by clicking on File (at top left) → New File (shortcut key “ctrl + n“)

Create New File in Python
Create New Python File

Then you will see an Editor window of will Open

Editor Window
Python 3 – Editor Window

2. SAVE PYTHON FILE

On the other hand, after creating a file write the program and click on File → Save, in short you can use shortcut key, that is, “ctrl + s“.

Save python File
Save python File

3. RUN CODE IN PYTHON

After saving code to see the Output run the code by using the menu Run → Run Module or the keyboard shortcut F5. As a result, the output is shown below.

Run Python File
Run Python File

PYTHON BASIC TERMS

Just printing ‘Hello World’ is not enough, is it? You need to do more than that. Yes, we want some input to manipulate and to get something out of it. We can achieve this using constants and variables.

1. LITERAL CONSTANTS IN PYTHON

For example, a literal constant is a number like 5, 1.23, 9.25e-3 or a string like ‘This is a string‘ or “It’s a string!“. It is called a literal because these are fixed value to be used in program. The number 2 always represents itself and nothing else as it is a constant because its value cannot be changed. Hence, all these are referred to as literal constants.

2. NUMBERS IN PYTHON 

Numbers are of three types that are integers, floating point and complex numbers.

  • An example of an integer is 2 which is just a whole number.
  • A few examples of floating point numbers (or floats for short) are 3.23 and 52.3E-4. The E notation indicates powers of 10. In this case, 52.3E-4 means 52.3 * 10^-4.
  • Complex numbers examples are (-5 + 4j) and (2.3 – 4.6j).

3. VARIABLES IN PYTHON 

Variables are exactly what the name implies, their value can be vary, i.e., you can store anything using a variable. They are just parts of your computer’s memory where you store some information. Unlike literal constants, you need some method of accessing these variables and hence you give them names, like

x = 45
s = "One of the String"

4. How to take input in Python 3?

To take an input, we have to write code as mentioned below. First take any variable to store the input here name, and then follow the syntax for input, input(‘Enter your name: ‘).

name = input('Enter your name: ')
print('Name is: ',name)
Write code in Editor Window
Write Python code in Editor Window

OUTPUT:

Here, you will be asked to write name, I wrote Mahendra, see it is marked red color under lined, will entered by user and can be any name or string. Then, press enter.

Then print(‘Name is: ‘, name) statement will be executed.

Input in python 3
Input in python 3

5. WORKING WITH STRING IN PYTHON 3

5.1 STRINGS

A string is a sequence of characters. Strings are basically just a bunch of words.

s = "One of the String"

where, s is a variable and “One of the String” is string.

5.2 SINGLE QUOTES

You can specify strings using single quotes such as

'Quote me on this'

All white space i.e. spaces and tabs are preserved as-is.

5.3 DOUBLE QUOTES

Strings in double quotes work exactly the same way as in single quotes. An example is

"What's your name?"

5.4 TRIPLE QUOTES

You can specify multi-line strings using triple quotes – (“”” (3 double quotes) or ”’ (3 single quotes)). However, you can use single quotes and double quotes freely within the triple quotes.

An example is:   

''' (This is the first line) This is a multi-line string.
(This is the second line) "What's your name?," I asked.
(This is the Third line) He said "Bond, James Bond." '''

6. DIFFERENT OPERATIONS ON STRING IN PYTHON 3

Suppose we have a string :

a = 'Hello, World'

Now , we perform different operations on above string.

6.1 TO PRINT PRINT CHARACTER AT INDEX “1”

a = 'Hello, World'
print(a[1])

OUTPUT:

Prints the character at position 2 (index 1) (remember that the first character position has index 0).

Print character at particular Index of String in Python
Print character at particular Index of String in Python

6.2 SUB-STRING from INDEX 2 to 4

a = 'Hello, World'
print(a[2:5])

(5 will not include, i.e. 5 – 2 = 3, it will print 3 character starting index from 2 that are 2, 3, 4)

OUTPUT:

sub string in python
Sub-string in python

6.3 TO FIND OUT LENGTH OF THE STRING IN PYTHON 3

Use len(string_variable), this method returns the length of the string in integer.

a = 'Hello, World'
print(len(a))

OUTPUT:

Length of string in python
Length of string in python

We can perform many types of operations apart from these operations on strings.

6.4 ESCAPE SEQUENCES (\)

Suppose you want to have a string which contains a single quote (‘), How will you specify this string?

For example, the string is: What’s your name? You cannot specify it as ‘What’s your name?’ because Python will get confused as where the string starts and ends. So, you will have to specify that this single quote does not indicate the end of the string.

This can be done with the help of what is called an escape sequence. Now, you can specify the string as

print('What\'s your name?')

OUTPUT:

Escape Sequences
Escape Sequences

Also Read: conditional statements in python

Also Learn: Arduino

Leave a Reply

Your email address will not be published. Required fields are marked *