0% found this document useful (0 votes)
4 views

Python

The document provides an introduction to Python programming, covering basic concepts such as string manipulation, input functions, variables, data types (integer, float, boolean), type checking, type conversion, and mathematical operators. It includes code examples illustrating each concept, along with explanations of common errors like indentation and type errors. Additionally, it discusses operator precedence in Python.

Uploaded by

shivamteli07
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Python

The document provides an introduction to Python programming, covering basic concepts such as string manipulation, input functions, variables, data types (integer, float, boolean), type checking, type conversion, and mathematical operators. It includes code examples illustrating each concept, along with explanations of common errors like indentation and type errors. Additionally, it discusses operator precedence in Python.

Uploaded by

shivamteli07
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Python

First Hello world program


#printing welcome hello world
print("Hello world!!")

String manipulation in python:


For day 1 we seen how to handle string manipulation in python like
concatenating strings
# concat strings
print("Hello " + "world")

After this we seen some errors that often come if you do some
mistakes
Indentation error : Generally comes when you give some extra space
which is not needed for given block

Input function in python


To take input from user we use input function in python

# input function in python


i=input("Hi What is your name")
print("Hi Your name is",i)

Variables in python
We use variables that acts as storing box for data
#variables in python
x=8
print(x)

length function in python


we use length function for seeing how many characters are present
in current string
Length start from 0 in python

Day 2

Strings
Number of characters inside double quotes
# String
name="Shivam"
print(name)

And we can access individual character by using following syntax:


# we can access this string using index
name2="Shivam2"
print(name2[0])

We can also access string characters by giving negative index


name2="Shivam2"
print(name2[0])
# accessing string using negative index
print(name2[-1])

String concatenation in python


We can concat two strings by using + operator in python
# String concatination in python
name1="Shivam"
name2="Teli"
name3=print("Your name will be"+" "+name1+" "+ name2)

Integer in python
Integer datatype in python is used to denote whole numbers in
python
# Integer
age=18
print(age)

Float
If we want to denote any number in python with floating points ex.
8.8 Then we se Float datatype representation
# Float
roll=8.8
print(roll)

Boolean:
In python Boolean datatype has fixed values that is true or false
# boolean
shivam=True
Teli=False
print(shivam)
print(Teli)

Type error ,Type Checking, Type Conversion


Type error
If we are passing any datatype which is not applicable for needed
functionality then we get type error
For example :
#Type error
print(len(1234))

After processing this code we will get error something like this :

For checking type in python we use following syntax


type(value)
#Type checking
print(type(123))
print(type("Shivam"))
print(type(True))
print(type(8.8)

Output:

Type conversion or Type casting in python


If we want to convert one datatype into another one then we use
type casting in python

# Type conversion
converted_int = int(number) # Convert string to integer
converted_float = float(decimal) # Convert string to float
converted_bool1 = bool(truthy) # Convert integer to boolean
(True)
converted_bool2 = bool(falsy) # Convert integer to boolean
(False)
converted_str = str(converted_float) # Convert float to string

# Display results with types


print("String to Integer:", converted_int, "Type:",
type(converted_int))
print("String to Float:", converted_float, "Type:",
type(converted_float))
print("Integer to Boolean (1):", converted_bool1, "Type:",
type(converted_bool1))
print("Integer to Boolean (0):", converted_bool2, "Type:",
type(converted_bool2))
print("Float to String:", converted_str, "Type:", type(converted_str))

Output :

Mathematical operators in python


Mathematical operators are used in python for performing various
mathematical operations
# Mathematical Operators in Python

# Addition
print("Addition:", 8 + 8 + 8) # Adds the numbers (8 + 8 + 8 = 24)
# Subtraction
print("Subtraction:", 8 - 8) # Subtracts 8 from 8 (8 - 8 = 0)
# Multiplication
print("Multiplication:", 8 * 8) # Multiplies 8 by 8 (8 * 8 = 64)
# Division
print("Division:", 8 / 3) # Divides 8 by 3, returns a float result
(8 / 3 = 2.666...)
# Floor Division (// operator)
print("Floor Division:", 8 // 3) # Divides 8 by 3, returns the integer
part (8 // 3 = 2)
# Modulus (Remainder)
print("Modulus (Remainder):", 8 % 8) # Divides 8 by 8, returns the
remainder (8 % 8 = 0)
# Exponentiation
print("Exponentiation:", 8 ** 2) # Raises 8 to the power of 2 (8^2 =
64)

Output :

Operator precedence in python

You might also like