0% found this document useful (0 votes)
13 views34 pages

Python Variables and Type Casting Guide

The document provides an introduction to variables, casting, strings, numbers, and input/output in Python. It discusses that Python does not require variable declaration, how variables are created and named, and examples of integer, float, and complex number variables. It also covers casting between data types, indexing and slicing strings, string methods like lower(), upper(), strip(), and replacing characters. The document demonstrates taking input from the user and performing operations on that input.

Uploaded by

Harshal Nigam
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views34 pages

Python Variables and Type Casting Guide

The document provides an introduction to variables, casting, strings, numbers, and input/output in Python. It discusses that Python does not require variable declaration, how variables are created and named, and examples of integer, float, and complex number variables. It also covers casting between data types, indexing and slicing strings, string methods like lower(), upper(), strip(), and replacing characters. The document demonstrates taking input from the user and performing operations on that input.

Uploaded by

Harshal Nigam
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Variables, Casting,

String

Gopal Gupta
[Link]/user/gopal_gupta
Let’s Start

>>> print("Hello, World!")


Hello, World!
Let’s Start

>>> print("Hello, World!")


Hello, World!
Comments
• Python has commenting capability for the purpose of in-code
documentation.
• Comments start with a #, and Python will render the rest of the line as a
comment:

#This is a comment.
print("Hello, World!")
Python Variables
• Unlike other programming languages, Python has no command for declaring
a variable.
• A variable is created the moment you first assign a value to it.

x=5
y = "John"
print(x)
print(y)
Variable Naming
• A variable name must start with a letter or the underscore character
• A variable name cannot start with a number
• A variable name can only contain alpha-numeric characters and
underscores (A-z, 0-9, and _ )
• Variable names are case-sensitive (age, Age and AGE are three different
variables)
Output Variables

x = "awesome"
print("Python is " + x)
Output Variables

x = "awesome"
print("Python is " + x)
Output Variables

x = "awesome"
print("Python is " + x)
Output Variables

x = "Python is "
y = "awesome"
z= x+y
print(z)
Output Variables

x=5
y = 10
print(x + y)
Output Variables

x=5
y = "John"
print(x + y)
Python Numbers

 int
 float
 complex
Example

x = 1 # int
y = 2.8 # float
z = 1j # complex
Example

x=1 <class 'int'>


y = 2.8 <class 'float'>
z = 1j <class 'complex'>

print(type(x))
print(type(y))
print(type(z))
Python Casting
y = int(2.8) # y will be 2
z = int("3") # z will be 3

y = float(2.8) # y will be 2.8


z = float("3") # z will be 3.0

x = str("s1") # x will be 's1'


y = str(2) # y will be '2'
z = str(3.0) # z will be '3.0'
Python Strings

a = "Hello, World!"
print(a[1]) # e
Python Strings

b = "Hello, World!"
print(b[2:5]) # llo
Python Strings

a = "Hello, World!"
print(a[1]) # e
Python Strings

a = "Hello, World!"
print(a[1]) # e
Python Strings
a = " Hello, World! "
print([Link]())
#The strip() method removes any whitespace from the
beginning or the end:
Python Strings

a = "Hello, World!"
print(len(a)) # 13
Python Strings

a = "Hello, World!"
print([Link]()) # hello, world!
Output Variables

a = "Hello, World!"
print([Link]())
Output Variables

a = "Hello, World!"
print([Link]("H", "J"))
#Jello, World!
Take Input

print("Enter your name: ")


x = input()
print("Hello, ", x)

x= input("Enter Name: ")


Take Input

print("Enter your name:")


x = input()
print("Hello, ", x)
Take Input

print("Enter your name:")


x = input()
print("Hello, ", x)
Take Input

print("Enter a No.: ")


x = input()
y=x + 5
print("Hello, ", y)
Take Input

print("Enter a No.: ")


x = input()
y=x + 5
print("Hello, ", y)
For You

x = input("Enter A no: ")


y = x*5;
print(y)
For You

x = input("Enter A no: ")


y = x*5;
print(y)
Have You Enjoy ?
Thank You

Continue …………

You might also like