Python Language Fundamentals
Variables & Data Types In Python
# A variable is a container for a value, which can be of various types
'''
This is a
multiline comment
or docstring (used to define a function's purpose)
can be single or double quotes
'''
"""
VARIABLE RULES:
- Variable names are case sensitive (name and NAME are different variables)
- Must start with a letter or an underscore
- Can have numbers but can not start with one
"""
# x = 1 # int
# y = 2.5 # float
# name = 'John' # str
# is_cool = True # bool
# Multiple assignments
x, y, name, is_cool = (1, 2.5, 'John', True)
# Basic math
a = x + y
# Casting
x = str(x)
y = int(y)
z = float(y)
print(type(z), z)
Strings & Formatting
# Strings in Python are surrounded by either single or double quotation marks. Let's look at string formatting and some string methods
name = 'John'
age = 37
# Concatenate
print('Hello, my name is ' + name + ' and I am ' + str(age))
# String Formatting
# Arguments by position
print('My name is {name} and I am {age}'.format(name=name, age=age))
# F-Strings (3.6+)
print(f'Hello, my name is {name} and I am {age}')
# String Methods
s = 'hello world'
# Capitalize string
print(s.capitalize())
# Make all uppercase
print(s.upper())
# Make all lower
print(s.lower())
# Swap case
print(s.swapcase())
# Get length
print(len(s))
# Replace
print(s.replace('world', 'everyone'))
# Count
sub = 'h'
print(s.count(sub))
# Starts with
print(s.startswith('hello'))
# Ends with
print(s.endswith('d'))
# Split into a list
print(s.split())
# Find position
print(s.find('r'))
# Is all alphanumeric
print(s.isalnum())
# Is all alphabetic
print(s.isalpha())
# Is all numeric
print(s.isnumeric())
Python List
# A List is a collection that is ordered and changeable. Allows duplicate members.
# Create list
numbers = [1, 2, 3, 4, 5]
fruits = ['Apples', 'Oranges', 'Grapes', 'Pears']
# Use a constructor
# numbers2 = list((1, 2, 3, 4, 5))
# Get a value
print(fruits[1])
# Get length
print(len(fruits))
# Append to list
fruits.append('Mangos')
# Remove from the list
fruits.remove('Grapes')
# Insert into position
fruits.insert(2, 'Strawberries')
# Change value
fruits[0] = 'Blueberries'
# Remove with pop
fruits.pop(2)
# Reverse list
fruits.reverse()
# Sort list
fruits.sort()
# Reverse sort
fruits.sort(reverse=True)
print(fruits)
Python Tuples
# A Tuple is a collection that is ordered and unchangeable. Allows duplicate members.
# Create tuple
fruits = ('Apples', 'Oranges', 'Grapes')
# Using a constructor
# fruits2 = tuple(('Apples', 'Oranges', 'Grapes'))
# Single value needs trailing comma
fruits2 = ('Apples',)
# Get value
print(fruits[1])
# Can't change the value
fruits[0] = 'Pears'
# Delete tuple
del fruits2
# Get length
print(len(fruits))
Python Sets
# A Set is a collection that's unordered and unindexed. No duplicate members.
# Create set
fruits_set = {'Apples', 'Oranges', 'Mango'}
# Check if in set
print('Apples' in fruits_set)
# Add to set
fruits_set.add('Grape')
# Remove from set
fruits_set.remove('Grape')
# Add duplicate
fruits_set.add('Apples')
# Clear set
fruits_set.clear()
# Delete
del fruits_set
print(fruits_set)
0 Replies to “Complete Python Tutorial For Freshers”
Leave a Reply
Your email address will not be published.