Basic Introduction To Python

CS Learning Study

What is Python?

Python is a popular programming language created by Guido van Rossum. A programming language is a type of written language that tells computers what to do in order to work. Apart from Python, there are many other programming languages like C, C++, Java, PHP, etc. By using these programming languages you can create your own software or applications.

Why Python is popular?

  • Python is very easy to learn even for beginners and newcomers.
  • Python has a simple syntax similar to English language.
  • Python works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc).
  • Moreover, Python has emerged as the default language for Artificial Intelligence and Data Science.

Python Command Line

Open your command prompt and type ‘python’

C:\Users\Your Name>python

Or, if the “python” command did not work, you can try “py”:

C:\Users\Your Name>py

From there you can write any python code

>>> a = 1

>>> a

1

>>> b = ‘Hi’

>>> b

‘Hi’

>>> v = ‘Hello World’

>>> v

‘Hello World’

>>> exit()

exit() or Ctrl-Z plus Return to exit

Python Comments

Comments starts with ‘#’ and Python ignores that line.

>>> a = 1

>>> a

1

>>> a = 2

>>> a

2

>>>

>>>

>>> a = 1

>>> a

1

>>> # a = 2

>>> a

1

>>> 

Comments can also be placed at the end of a line, and Python will ignore the rest of the line.

Use of Comments

  • Comments can be used to explain Python code.
  • Comments can be used to make the code more readable.
  • Comments can be used to prevent execution when testing code.

Variables:

Variables are used for storing values.

In previous command line examples a, b and v are called variables.

myvariablename = ‘Byte-man’

You can use different techniques to make variables more readable. Most popular are:

Camel – myVariabeName = ‘Byte-man’

Pascal – MyVariableName = ‘Byte-man’

Snake – my_variable_name = ‘Byte-man’

Assigning variables values

a = 1
b = 2
c = 3
print(a, b, c)

a, b, c = 1, 2, 3
print(a, b, c)

in both cases the output is same, it is:

1 2 3

Data Types

Variables have different types. Python has various data types built-in by default. Notable ones:
Text Type: str
Numeric Types: int, float
Sequence Types: list, tuple
Mapping Type: dict
Set Types: set
Boolean Type: bool

a = 1
print(type(a))

<class ‘int’>

b=’hi’
print(type(b))

<class ‘str’>

c = True
print(type(c))

<class ‘bool’>

d = [1, 2, 3]
print(type(d))

<class ‘list’>

e = (1, 2, 3)
print(type(e))

<class ‘tuple’>

f = 3.14
print(type(f))

<class ‘float’>

Type Casting

Sometimes you have to specify a type of a variable or want a variable into specific type. This can be done by casting.

input => output

float(9) => 9.0

int(9.9) => 9

int(‘9’) => 9

int(‘V’) => gives error

str(9) => “9”

str(9.99) => “”9.99”

Bool = Boolean = True / False

type(True) => bool

bool(1) => True

bool(0) => False

int(False) => 0

int( True) => 1

Mathematical Operations

Operators are used to perform operations on variables and values.

9 + 108 + 365

Here 9, 108, & 365 are Operators while + is Operand.

Operator Name Example

  • Addition x + y
  • Subtraction x – y
  • Multiplication x * y
  • / Division x / y
  • % Modulus x % y
  • ** Exponentiation x ** y
  • // Floor division x // y

Example

x = 1/1

print(x) => 1.0

type(x) => float

another example

x = 1//1

print(x) => 1

type(x) => int

x = 1//2

print(x) => 0

x = 2//1

print(x) => 2

Comparison operators

Equal -> x == y
Not equal -> x != y
Greater than -> x > y
Less than -> x < y Greater than or equal to -> x >= y
Less than or equal to -> x <= y

String

In python, a string is a sequence of characters. (Double quotes or single quotes)

‘Hi Earth’

Hi Earth
01234567
-8-7-6-5-4-3-2-1

Take another example:

name = ‘Captain Planet’

Captain Planet
012345678910111213
  • name[starting point: length from given start point]

name[0:4] = Capt

name[8:12] = Plan

:: is called Stride

  • name[ starting point : limit point : gap ]

name[0:5:2] = Cpa (here 5 is indicating index should be less than 5 only)

name[1::2] = ati lnt (here 1 is starting point & 2 is gaps)

name[::2] = CpanPae

  • To find the length of a string,, len() function is used.

len(“Captain Planets”) = 15

  • Multiplying string with any integer(N) will gives new string repeated N number of times.

print(‘hi’ * 3) or print(3 * ‘hi ‘) => hi hi hi

  • By using ‘+’ between two strings you can get new string with joining both

example = ‘Hello ‘ + ‘World’

print(example) => Hello World

  • String is immutable.

Name = “Captain Planets”

Name[0] = “P” -> Gives TypeError as String is immutable.

  • Escape character s a backslash \ followed by the character you want to insert. For example:

\n -> new line character

print(“Captain Planets \n is the best”);

output -> Captain Planets

                    is the best

\t -> tab

\\ -> for representing \

print(“Captain \\ Planets”) -> Captain \ Planets

print(r “Captain \n Planets”) -> Captain \n Planets

upper()-> The upper() methods returns the upper cased string from the given string. It converts all lowercase characters to uppercase. Let’s take one example…

str5 = Hello, how are you?

print(str5.upper())

Your output will be:

HELLO, HOW ARE YOU?

lower() -> The lower() methods returns the lower cased string from the given string. It converts all uppercase characters to lowercase.

Let’s take another example…

str6 = ‘Nitrogen is a nonmetal whose symbol is N’

print(str6.lower())

Your output will be:

nitrogen is a nonmetal whose symbol is n

replace() -> The replace() method return new string with replacing the characters with new ones if specified phrase is found.

A = “Green Earth”
B = A.upper() -> GREEN EARTH
B =A.replace(‘Green’, ‘Save’) -> Save Earth

find() -> The find() method finds the first occurrence of the specified value.

Captain Planet
012345678910111213

Name = “Captain Planets”
Name.find(‘in’) = 5 (starting point)
Name.find(‘Plan’) = 8
Name.find(‘Caps’) = -1 -> Unavailable so -1

Continue Reading... Basic Introduction To Python II