Python Tutorial for Beginners Part 2

Python Tutorial for Beginners Part 2

Strings:-

Strings include letters, special characters, spaces, digits, etc.
It is enclosed in single quotes ( ‘ ‘ ) or double quotes ( ” ” ).
str = "Cyber Connaught"

We can concatenate strings

str = "Cyber Connaught"
welcome_str = "Welcome to our website"
concate_str = welcome_str + str
print(concate_str)
> Welcome to our websiteCyber Connaught

Covered Topics

  • Strings
  • Indexing
  • Slicing
  • Input
  • Comparison Operators
  • Logical Operators

To get it in a proper way

  1. Concate_str = welcome_str + " " + str
  2. Just give some space in welcome_str at end
welcome_str = "Welcome to our website"
print(concate_str = welcome_str + " " + str) 
> Welcome to our website Cyber Connaught
welcome_str = "Welcome to our website "
concate_str = welcome_str + str
print(concate_str)
> Welcome to our website Cyber Connaught

One of the operations on a string is defined in python docs.

str = "Conrado"
str_3 = "My name is " + str * 3 + "!"
> My name is ConradoConradoConrado!

We can calculate the length of the string by len().

name = "Conrado"
len(str)
> 7
sur_name = "Torres"
full_name = name + " " + sur_name
len(full_name)
> 14

Indexing:-

We can get a specific character as a variable or output in a string by indexing the string. An index is a position in a string. In Python indexing start from 0.

The first character is at an index of 0
The second character is at index 1
and goes on…..

name = "Conrado Torres"
ConradoTorres
01234567891011121314
-15-14-13-12-11-10-9-8-7-6-5-4-3-2-1
print(name[2])
>n
print(name[6])
>o
print(name[12])
>e

How do you print the last character of a string?
str = "pneumonoultramicroscopicsilicovolcanokoniosis"
We can use two simple methods to get the last character.

char = str[len(str) - 1]
print(char)
> s
print(str[-1])
> s

python uses negative indexing to count from the end.

Slicing:-

We can get a subset of characters by slicing.
steps:-

  • get the starting index of the slice
  • separate with a column
  • get the ending index of the slice

str [ i : j ] gives you characters at index
i , i + 1 , i + 2 , … , j – 1

Once go through the following tables and the following problems.

name = "Cyber Connaught"
CyberConnaught
0123456789101112131415
-16-15-14-13-12-11-10-9-8-7-6-5-4-3-2-1

Examples –

name[6:13]
> ‘Connaug’
name[0:5]
> ‘Cyber’
name[6:15]
> ‘Connaught’
name[6:-2]
> ‘Connaug’
name[:5]
> ‘Cyber’
name[6:]
> ‘Connaught’
1)name [4:7]
‘r C’
name[-11:-8]
‘r C’
name[4:-8]
‘r C’
name[-11:7]
‘r C’
2)name[:]
‘Cyber Connaught’
name[::]
‘Cyber Connaught’
name[0:]
‘Cyber Connaught’
name[:15]
‘Cyber Connaught’

Input:-

variable = input("Write some thing you want from user")

Example –

name = input("Enter your name..")
     
#input statement
> Enter your name.. Cyber Connaught  #you have to enter name

# Now, name = "Cyber Connaught"
print("Your name is " + name)

> Your name is Cyber Connaught

Input gives you a string so you must cast if working with numbers.

Examples –

num = input("Ënter the number ")
> Enter the number 3
print(3*num)
> 333
num = int(input("Ënter the number "))
> Enter the number 3
print(3*num)
> 9

Write a program that will ask your name and your age and then print it out like
“My name is ‘name’ and I am ‘age’ years old”

name = input("Enter your name ")
age = input("Enter your age ")
print("My name is " + name + " and I am " + age + " years old.")

Comparison Operators:-

Let’s take i and j variable names. The output of the comparison evaluates to a Boolean value.

1.i > ji greater than j5 > 3True
2.i >= ji greater than or equal to j5 >= 35 >= 5 True
3.i < ji less than j3 < 5True
4.i <= ji less than or equal to j3 <= 55 <= 5True
5.i == ji equal to j3 == 33 == 3.0True
6.i != ji not equal to j3 != 5 True

Logical Operators:-

There are three operators not, and, or

  • not negates the Boolean value
  • and returns True only if both statements are True.
  • or returns False only if both statements are False.

Just here for better understanding take :- ( True = 1 ) , ( False = 0 )

Sr.noabnot anot ba and ba or b
1001100
2011001
3100101
4110011

Examples –

a , b ,c = 6 , 8 , 4
a > b
## False
not (a > b)
## True
(a > b) and (b > a)
## False
(a > b) or (b > a)
## True
(a < c) or ((b > c) and not (a < c))
## True
work_time = 15
sleep_time = 8
print(sleep_time < work_time)
## True
work_time = 15
sleep_time = 8
print(sleep_time > work_time)
## False
sports = True
games = False
both = sports and games
print(both)
## False

Here sports can get its value by another function or equation by a sequence of logical steps.

sports = 3>2 ( Equation )
games = False
both = sports and games
print(both)
## False

Leave a comment