Docsity

Python syntax Function to get length of string, Exams of Health sciences

Toggle sidebar
bg1
Python syntax
Function to get length of string - ANSWER...-len()
String method to convert to uppercase - ANSWER...-.upper()
String method to convert to lowercase - ANSWER...-.lower()
String interpolation syntax - "Hi, #{name}" - ANSWER...-"Hi, %s" % (name)
Get current time - ANSWER...-from datetime import datetime
datetime.now()
&& operator - ANSWER...-and
|| operator - ANSWER...-or
! operator - ANSWER...-not
order of ops between and, not, or - ANSWER...-not, and, or
Control state syntax - ANSWER...-if something:
elif something:
else something
Push to the end of a list - ANSWER...-.append()
Create a new list - ANSWER...-x = []
Array - ANSWER...-list
Object - ANSWER...-dict
Create a new dict - ANSWER...-x = {}
For loop syntax - ANSWER...-for color in colors:
(indent) print(color)
Variable for module's name - ANSWER...-__name__
Import module in repl (words.py) - ANSWER...-import words
pf2

Partial preview of the text

Download Python syntax Function to get length of string and more Exams Health sciences in PDF only on Docsity!

Python syntax

Function to get length of string - ANSWER...-len() String method to convert to uppercase - ANSWER...-.upper() String method to convert to lowercase - ANSWER...-.lower() String interpolation syntax - "Hi, #{name}" - ANSWER...-"Hi, %s" % (name) Get current time - ANSWER...-from datetime import datetime datetime.now() && operator - ANSWER...-and || operator - ANSWER...-or ! operator - ANSWER...-not order of ops between and, not, or - ANSWER...-not, and, or Control state syntax - ANSWER...-if something: elif something: else something Push to the end of a list - ANSWER...-.append() Create a new list - ANSWER...-x = [] Array - ANSWER...-list Object - ANSWER...-dict Create a new dict - ANSWER...-x = {} For loop syntax - ANSWER...-for color in colors: (indent) print(color) Variable for module's name - ANSWER...-name Import module in repl (words.py) - ANSWER...-import words

Import fetch_words and print_words from words.py - ANSWER...-from words import (fetch_words, print_words) Import all functions from words.py - ANSWER...-from words import * Check if runtime context is script or module - ANSWER...-if name is "main": is vs == - ANSWER...-p = [1,2] q = [1,2] p == q --> true p is q --> false Set default argument b to 5 - ANSWER...-def someFunction(b=5): Rewrite border("Hello there", "") to rearrange argument order (first argument is message, second is border) - ANSWER...-border(border="", message="Hello there") Tuple - ANSWER...-Immutable sequence of arbitrary objects. Like a list, but cannot add or change. Often used for multiple return values. Create new tuple "t" with one item - "hi" - ANSWER...-t = ("hi",) Switch values of a and b - ANSWER...-a,b = b,a Concatenate "hello" and "there" - ANSWER...-"".join(["hello", "there"]) Break up "en-ca" by the hyphen. Save as language and country - ANSWER...-language, country = "en-ca".partition("-") "Hi [name], my favorite color is [color]" - format - ANSWER...-"Hi {0}, my favorite color is {1}".format("Kara", "blue") Create an array of numbers 0 to 10, by twos - ANSWER...-list(range(0,12,2)) Copy a list "x" using slicing - ANSWER...-x[:] Iterate over keys and values in colors dict - ANSWER...-for key, value in colors.items(): Set - ANSWER...-unordered collection of immutable objects p = {10, 20, 30} Create a new set - ANSWER...-p = set()