0% found this document useful (0 votes)
6 views9 pages

Python Basics

This document provides an overview of basic Python programming concepts, including printing to the console, variable assignment, data types, and various operators. It covers valid and invalid variable names, arithmetic, comparison, logical, bitwise, assignment, identity, membership, and ternary operators. Additionally, it discusses operator precedence and how to access string elements.

Uploaded by

tej.ch
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)
6 views9 pages

Python Basics

This document provides an overview of basic Python programming concepts, including printing to the console, variable assignment, data types, and various operators. It covers valid and invalid variable names, arithmetic, comparison, logical, bitwise, assignment, identity, membership, and ternary operators. Additionally, it discusses operator precedence and how to access string elements.

Uploaded by

tej.ch
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

# Python Program to Print Hello World!

print("Hello World!")

Python's input() function is used to take user input. By default, it returns the user input
in form of a string.

name = input("Enter your name: ")

print("Hello,", name, "! Welcome!")

Python Variables

x=5

name = "Samantha"

print(x)

print(name)

Valid Variables

age = 21

_colour = "lilac"

total_score = 90

Invalid Variables

1name = "Error" # Starts with a digit

class = 10 # 'class' is a reserved keyword

user-name = "Doe" # Contains a hyphen


Assigning Values to the variables

x=5

y = 3.14

z = "Hi"

Multiple Assignments

Example 1:

a = b = c = 100

print(a, b, c)

Example 2:

x, y, z = 1, 2.5, "Python"

print(x, y, z)

Printing Variables

s = "Brad"

print(s)

s = "Anjelina"

age = 25

city = "New York"

print(s, age, city)


Type of variable

n = 42

f = 3.14

s = "Hello, World!"

li = [1, 2, 3]

d = {'key': 'value'}

bool = True

print(type(n))

print(type(f))

print(type(s))

print(type(li))

print(type(d))

print(type(bool))

Deleting variables

x = 10

del x

print(x)

Counting Characters in a String

word = "Python"

length = len(word)

print("Length of the word:", length)


Arithmetic Operators

# Variables

a = 15

b=4

# Addition

print("Addition:", a + b)

# Subtraction

print("Subtraction:", a - b)

# Multiplication

print("Multiplication:", a * b)

# Division

print("Division:", a / b)

# Floor Division

print("Floor Division:", a // b)

# Modulus

print("Modulus:", a % b)

# Exponentiation

print("Exponentiation:", a ** b)
Comparison Operators

a = 13

b = 33

print(a > b)

print(a < b)

print(a == b)

print(a != b)

print(a >= b)

print(a <= b)

Logical Operators

a = True

b = False

print(a and b)

print(a or b)

print(not a)

Bitwise Operators

a = 10

b=4

print(a & b)

print(a | b)

print(~a)

print(a ^ b)
print(a >> 2)

print(a << 2)

Assignment Operators

a = 10

b=a

print(b)

b += a

print(b)

b -= a

print(b)

b *= a

print(b)

b <<= a

print(b)

Identity Operators

a = 10

b = 20

c=a

print(a is not b)

print(a is c)
Membership Operators

x = 24

y = 20

list = [10, 20, 30, 40, 50]

if (x not in list):

print("x is NOT present in given list")

else:

print("x is present in given list")

if (y in list):

print("y is present in given list")

else:

print("y is NOT present in given list")

Ternary Operators

a, b = 10, 20

min = a if a < b else b

print(min)

Operator Precedence

expr = 10 + 20 * 30
print(expr)

name = "Alex"

age = 0

if name == "Alex" or name == "John" and age >= 2:

print("Hello! Welcome.")

else:

print("Good Bye!!")

Getting the list of Keywords in Python

import keyword

print("The list of keywords are : ")

print([Link])

Python Data Types


Numeric Data Types

a=5

print(type(a))

b = 5.0

print(type(b))

c = 2 + 4j

print(type(c))

String Data Type

s = 'Welcome to the Geeks World'

print(s)

# check data type

print(type(s))

# access string with index

print(s[1])

print(s[2])

print(s[-1])

You might also like